class HoneyFormat::Row

Default row builder

Public Class Methods

call(row) click to toggle source

Create a row @return [Row] returns an instantiated Row @example

row_klass = Row.new(:id, :username)
row = row_klass.call('1', 'buren')
# => #<struct id="1", username="buren">
# File lib/honey_format/matrix/row.rb, line 12
def self.call(row)
  new(*row)
end

Public Instance Methods

inspect() click to toggle source

Describe the contents of this row in a string. @return [String] content of this row

# 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
Also aliased as: to_s
to_csv(columns: nil) click to toggle source

Represent row as CSV @param columns [Array<Symbol>, Set<Symbol>, NilClass]

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

@return [String] CSV-string representation.

# 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
to_s()
Alias for: inspect

Private Instance Methods

to_csv_value(column) click to toggle source

Returns the column in CSV format @param [Symbol] column name @return [String] column value as CSV string

# File lib/honey_format/matrix/row.rb, line 48
def to_csv_value(column)
  value = public_send(column)
  return if value.nil?
  return value.to_csv if value.respond_to?(:to_csv)

  value.to_s
end