class HoneyFormat::Rows

Represents rows.

Public Class Methods

new(rows, columns, builder: nil, type_map: {}, pre_built_rows: false) click to toggle source

Returns array of cleaned strings. @return [Rows] new instance of Rows. @param [Array] rows the array of rows. @param [Array<Symbol>] columns the array of column symbols. @param type_map [Hash] map of column_name => type conversion to perform. @param pre_built_rows [boolean] whether the rows come pre-built @raise [RowError] super class of errors raised when there is a row error. @raise [EmptyRowColumnsError] raised when there are no columns. @raise [InvalidRowLengthError] raised when row has more columns than header columns.

# File lib/honey_format/matrix/rows.rb, line 19
def initialize(rows, columns, builder: nil, type_map: {}, pre_built_rows: false)
  @columns = columns
  if pre_built_rows
    @rows = rows
  else
    builder = RowBuilder.new(@columns, builder: builder, type_map: type_map)
    @rows = prepare_rows(builder, rows)
  end
end

Public Instance Methods

+(other) click to toggle source

Returns the rows added together. @return [Rows] the two sets of Rows added together. @param [Rows] the other Rows object.

# File lib/honey_format/matrix/rows.rb, line 44
def +(other)
  if columns != columns.union(other.columns)
    raise ArgumentError, "can't added two sets of rows with different columns"
  end

  rows = @rows + other.rows_data
  self.class.new(rows, columns, pre_built_rows: true)
end
[](index) click to toggle source

Return element at given position. @return [Row] of rows. @param [Integer] the index to return.

# File lib/honey_format/matrix/rows.rb, line 70
def [](index)
  @rows[index]
end
columns() click to toggle source

Row columns @return [Array<Symbol>] of column identifiers.

# File lib/honey_format/matrix/rows.rb, line 31
def columns
  @columns
end
each(&block) click to toggle source

@yield [row] The given block will be passed for every row. @yieldparam [Row] a row in the CSV file. @return [Enumerator]

If no block is given, an enumerator object will be returned.
# File lib/honey_format/matrix/rows.rb, line 57
def each(&block)
  @rows.each(&block)
end
empty?() click to toggle source

Returns true if rows contains no elements. @return [true, false] true if rows contains no elements.

# File lib/honey_format/matrix/rows.rb, line 37
def empty?
  @rows.empty?
end
length() click to toggle source

Return the number of rows @return [Integer] the number of rows

# File lib/honey_format/matrix/rows.rb, line 76
def length
  @rows.length
end
Also aliased as: size
size()
Alias for: length
to_a() click to toggle source

Returns rows as array. @return [Array] of rows.

# File lib/honey_format/matrix/rows.rb, line 63
def to_a
  @rows
end
to_csv(columns: nil) { |row| ... } click to toggle source

@param columns [Array<Symbol>, Set<Symbol>, NilClass]

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

@yield [row]

each row - return truthy if you want the row to be included in the output

@yieldparam [Row] row @return [String] CSV-string representation. @example with selected columns

rows.to_csv(columns: [:id, :country])

@example with selected rows

rows.to_csv { |row| row.country == 'Sweden' }

@example with both selected columns and rows

csv.to_csv(columns: [:id, :country]) { |row| row.country == 'Sweden' }
# File lib/honey_format/matrix/rows.rb, line 93
def to_csv(columns: nil, &block)
  # Convert columns to Set for performance
  columns = Set.new(columns.map(&:to_sym)) if columns
  csv_rows = []
  each do |row|
    if !block || yield(row)
      csv_rows << row.to_csv(columns: columns)
    end
  end
  csv_rows.join
end

Protected Instance Methods

rows_data() click to toggle source
# File lib/honey_format/matrix/rows.rb, line 107
def rows_data
  @rows
end

Private Instance Methods

prepare_rows(builder, rows) click to toggle source
# File lib/honey_format/matrix/rows.rb, line 113
def prepare_rows(builder, rows)
  built_rows = []
  rows.each do |row|
    # ignore empty rows
    next if row.nil? || row.empty? || row == [nil]

    built_rows << builder.build(row)
  end
  built_rows
end