Class: Nazar::Formatter::Struct
- Inherits:
-
Object
- Object
- Nazar::Formatter::Struct
- Defined in:
- lib/nazar/formatter/struct.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cells ⇒ Object
- #headers ⇒ Object
-
#initialize(item) ⇒ Struct
constructor
A new instance of Struct.
- #summary ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(item) ⇒ Struct
Returns a new instance of Struct.
6 7 8 9 |
# File 'lib/nazar/formatter/struct.rb', line 6 def initialize(item) @collection = item.is_a?(Array) ? item : [item] @attributes = @collection.first.to_h.keys end |
Class Method Details
.valid?(data) ⇒ Boolean
11 12 13 |
# File 'lib/nazar/formatter/struct.rb', line 11 def self.valid?(data) valid_struct?(data) || (data.is_a?(Array) && data.all? { valid_struct?(_1) }) end |
.valid_struct?(data) ⇒ Boolean
15 16 17 |
# File 'lib/nazar/formatter/struct.rb', line 15 def self.valid_struct?(data) data.is_a?(::Struct) || data.is_a?(::OpenStruct) end |
Instance Method Details
#cells ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/nazar/formatter/struct.rb', line 27 def cells @cells ||= @collection.map do |item| item.to_h.values.map do |value| CellFormatter.new(value, type: nil).format end end end |
#headers ⇒ Object
23 24 25 |
# File 'lib/nazar/formatter/struct.rb', line 23 def headers HeadersFormatter.new(@attributes).format end |
#summary ⇒ Object
35 36 37 |
# File 'lib/nazar/formatter/struct.rb', line 35 def summary false end |
#valid? ⇒ Boolean
19 20 21 |
# File 'lib/nazar/formatter/struct.rb', line 19 def valid? @attributes.any? end |