Class: HoneyFormat::Rows
- Inherits:
-
Object
- Object
- HoneyFormat::Rows
- Includes:
- Enumerable
- Defined in:
- lib/honey_format/matrix/rows.rb
Overview
Represents rows.
Instance Method Summary collapse
-
#+(other) ⇒ Rows
Returns the rows added together.
-
#[](index) ⇒ Row
Return element at given position.
-
#columns ⇒ Array<Symbol>
Row columns.
-
#each {|row| ... } ⇒ Enumerator
If no block is given, an enumerator object will be returned.
-
#empty? ⇒ true, false
Returns true if rows contains no elements.
-
#initialize(rows, columns, builder: nil, type_map: {}, pre_built_rows: false) ⇒ Rows
constructor
Returns array of cleaned strings.
-
#length ⇒ Integer
(also: #size)
Return the number of rows.
-
#to_a ⇒ Array
Returns rows as array.
-
#to_csv(columns: nil) {|row| ... } ⇒ String
CSV-string representation.
Constructor Details
#initialize(rows, columns, builder: nil, type_map: {}, pre_built_rows: false) ⇒ Rows
Returns array of cleaned strings.
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.
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.
70 71 72 |
# File 'lib/honey_format/matrix/rows.rb', line 70 def [](index) @rows[index] end |
#columns ⇒ Array<Symbol>
Row columns
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.
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.
37 38 39 |
# File 'lib/honey_format/matrix/rows.rb', line 37 def empty? @rows.empty? end |
#length ⇒ Integer Also known as: size
Return the number of rows
76 77 78 |
# File 'lib/honey_format/matrix/rows.rb', line 76 def length @rows.length end |
#to_a ⇒ Array
Returns rows as array.
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.
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 |