Class: Nazar::Formatter::Generic

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Generic

Returns a new instance of Generic.



8
9
10
11
# File 'lib/nazar/formatter/generic.rb', line 8

def initialize(collection)
  @collection = Array(collection)
  @item = collection.first
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/nazar/formatter/generic.rb', line 6

def collection
  @collection
end

Class Method Details

.valid?(data) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/nazar/formatter/generic.rb', line 29

def self.valid?(data)
  return false unless data.is_a?(Enumerable)

  item = data&.first
  item.respond_to?(:keys) && item.respond_to?(:values)
end

Instance Method Details

#cellsObject



17
18
19
20
21
22
23
# File 'lib/nazar/formatter/generic.rb', line 17

def cells
  @cells ||= collection.map do |item|
    item.values.map do |value|
      CellFormatter.new(value).format
    end
  end
end

#headersObject



13
14
15
# File 'lib/nazar/formatter/generic.rb', line 13

def headers
  HeadersFormatter.new(raw_headers).format
end

#summaryObject



25
26
27
# File 'lib/nazar/formatter/generic.rb', line 25

def summary
  collection.size
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/nazar/formatter/generic.rb', line 36

def valid?
  !!item && !raw_headers.empty?
end