Skip to content

Commit

Permalink
Correctly handle structs
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzak committed Jul 1, 2024
1 parent 9ab049b commit 3a654ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/nazar/formatter/active_record_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(collection)
end

def self.valid?(data)
return false if data.is_a?(Struct) || (defined?(OpenStruct) && data.is_a?(OpenStruct))
return false if data.is_a?(::Struct) || (defined?(OpenStruct) && data.is_a?(OpenStruct))

data.is_a?(ActiveRecord::Associations::CollectionProxy) ||
data.is_a?(ActiveRecord::Relation) ||
Expand Down
19 changes: 13 additions & 6 deletions lib/nazar/formatter/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@ module Nazar
module Formatter
class Struct
def initialize(item)
@collection = Array(item)
@attributes = item.to_h.keys
@item = item
@collection = item.is_a?(Array) ? item : [item]
@attributes = @collection.first.to_h.keys
end

def self.valid?(data)
valid_struct?(data) || (data.is_a?(Array) && data.all? { valid_struct?(_1) })
end

def self.valid_struct?(data)
data.is_a?(::Struct) || data.is_a?(::OpenStruct)
end

def valid?
true
@attributes.any?
end

def headers
HeadersFormatter.new(attributes).format
HeadersFormatter.new(@attributes).format
end

def cells
@cells ||= @collection.map do |item|
item.each_pair do |_, value|
item.to_h.values.map do |value|
CellFormatter.new(value, type: nil).format
end
end
end

def summary
false
end
end
end
end
2 changes: 1 addition & 1 deletion lib/nazar/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Nazar
VERSION = '1.3.0'
VERSION = '1.3.1'
end
2 changes: 1 addition & 1 deletion spec/nazar/formatter/generic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe Nazar::Formatter::Generic do
subject { described_class.new(collection) }

let(:struct) { Struct.new(:name, :age) }
let(:struct) { ::Struct.new(:name, :age) }

let(:john_hash) { { name: 'John', age: 18 } }
let(:jane_hash) { { name: 'Jane', age: 25 } }
Expand Down
24 changes: 21 additions & 3 deletions spec/nazar/view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
end

context 'with Struct' do
let(:data) { Struct.new(:id, :name).new(1, 'foo') }
let(:data) { ::Struct.new(:id, :name).new(1, 'foo') }

context 'without loaded extension' do
it do
Expand All @@ -81,8 +81,26 @@
unload_struct!
end

it do
expect(subject).to be_supported_data
context 'with valid Struct' do
it do
expect(subject).to be_supported_data
end
end

context 'with valid OpenStruct' do
let(:data) { OpenStruct.new(id: 1, name: 'foo') }

it do
expect(subject).to be_supported_data
end
end

context 'with an empty OpenStruct' do
let(:data) { OpenStruct.new }

it do
expect(subject).not_to be_supported_data
end
end
end
end
Expand Down

0 comments on commit 3a654ba

Please sign in to comment.