Class: HoneyFormat::Rows

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/honey_format/matrix/rows.rb

Overview

Represents rows.

Instance Method Summary collapse

Constructor Details

#initialize(rows, columns, builder: nil, type_map: {}, pre_built_rows: false) ⇒ Rows

Returns array of cleaned strings.

Parameters:

  • rows (Array)

    the array of rows.

  • columns (Array<Symbol>)

    the array of column symbols.

  • type_map (Hash) (defaults to: {})

    map of column_name => type conversion to perform.

  • pre_built_rows (boolean) (defaults to: false)

    whether the rows come pre-built

Raises:



19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#+(other) ⇒ Rows

Returns the rows added together.

Parameters:

  • the (Rows)

    other Rows object.

Returns:

  • (Rows)

    the two sets of Rows added together.



44
45
46
47
48
49
50
51
# 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) ⇒ Row

Return element at given position.

Parameters:

  • the (Integer)

    index to return.

Returns:

  • (Row)

    of rows.



70
71
72
# File 'lib/honey_format/matrix/rows.rb', line 70

def [](index)
  @rows[index]
end

#columnsArray<Symbol>

Row columns

Returns:

  • (Array<Symbol>)

    of column identifiers.



31
32
33
# File 'lib/honey_format/matrix/rows.rb', line 31

def columns
  @columns
end

#each {|row| ... } ⇒ Enumerator

Returns If no block is given, an enumerator object will be returned.

Yields:

  • (row)

    The given block will be passed for every row.

Yield Parameters:

  • a (Row)

    row in the CSV file.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.



57
58
59
# File 'lib/honey_format/matrix/rows.rb', line 57

def each(&block)
  @rows.each(&block)
end

#empty?true, false

Returns true if rows contains no elements.

Returns:

  • (true, false)

    true if rows contains no elements.



37
38
39
# File 'lib/honey_format/matrix/rows.rb', line 37

def empty?
  @rows.empty?
end

#lengthInteger Also known as: size

Return the number of rows

Returns:

  • (Integer)

    the number of rows



76
77
78
# File 'lib/honey_format/matrix/rows.rb', line 76

def length
  @rows.length
end

#to_aArray

Returns rows as array.

Returns:

  • (Array)

    of rows.



63
64
65
# File 'lib/honey_format/matrix/rows.rb', line 63

def to_a
  @rows
end

#to_csv(columns: nil) {|row| ... } ⇒ String

Returns CSV-string representation.

Examples:

with selected columns

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

with selected rows

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

with both selected columns and rows

csv.to_csv(columns: [:id, :country]) { |row| row.country == 'Sweden' }

Parameters:

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

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

Yields:

  • (row)

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

Yield Parameters:

Returns:

  • (String)

    CSV-string representation.



93
94
95
96
97
98
99
100
101
102
103
# 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