Class: HoneyFormat::Row

Inherits:
Struct
  • Object
show all
Defined in:
lib/honey_format/matrix/row.rb

Overview

Default row builder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(row) ⇒ Row

Create a row

Examples:

row_klass = Row.new(:id, :username)
row = row_klass.call('1', 'buren')
# => #<struct id="1", username="buren">

Returns:

  • (Row)

    returns an instantiated Row



12
13
14
# File 'lib/honey_format/matrix/row.rb', line 12

def self.call(row)
  new(*row)
end

Instance Method Details

#inspectString Also known as: to_s

Describe the contents of this row in a string.

Returns:

  • (String)

    content of this row



31
32
33
34
35
36
37
38
39
40
# File 'lib/honey_format/matrix/row.rb', line 31

def inspect
  attributes = members.map do |field|
    value = self[field]
    value = "\"#{value}\"" if value.is_a?(String)

    [field, value].join('=')
  end.join(', ')

  "#<Row #{attributes}>"
end

#to_csv(columns: nil) ⇒ String

Represent row as CSV

Parameters:

  • columns (Array<Symbol>, Set<Symbol>, NilClass) (defaults to: nil)

    the columns to output, nil means all columns (default: nil)

Returns:

  • (String)

    CSV-string representation.



20
21
22
23
24
25
26
27
# File 'lib/honey_format/matrix/row.rb', line 20

def to_csv(columns: nil)
  attributes = members
  attributes = columns & attributes if columns

  row = attributes.map! { |column| to_csv_value(column) }

  ::CSV.generate_line(row)
end