Class: Nazar::Formatter::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/nazar/formatter/struct.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (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

Returns:

  • (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

#cellsObject



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

#headersObject



23
24
25
# File 'lib/nazar/formatter/struct.rb', line 23

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

#summaryObject



35
36
37
# File 'lib/nazar/formatter/struct.rb', line 35

def summary
  false
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/nazar/formatter/struct.rb', line 19

def valid?
  @attributes.any?
end