Class: HoneyFormat::Matrix
- Inherits:
-
Object
- Object
- HoneyFormat::Matrix
- Defined in:
- lib/honey_format/matrix/matrix.rb
Overview
Represents Matrix.
Direct Known Subclasses
Instance Method Summary collapse
-
#columns ⇒ Array<Symbol>
Matrix columns converted from the original Matrix header.
-
#each_row {|row| ... } ⇒ Enumerator
Itereate over each row.
-
#header ⇒ Header
Original matrix header.
-
#initialize(matrix, header: nil, header_converter: HoneyFormat.header_converter, header_deduplicator: HoneyFormat.config.header_deduplicator, row_builder: nil, type_map: {}) ⇒ Matrix
constructor
Instantiate Matrix.
-
#rows ⇒ Rows
Return rows.
-
#to_csv(columns: nil) {|row| ... } ⇒ String
Convert matrix to CSV-string.
-
#type_map ⇒ Hash<Symbol, Symbol>
Matrix type map used.
Constructor Details
#initialize(matrix, header: nil, header_converter: HoneyFormat.header_converter, header_deduplicator: HoneyFormat.config.header_deduplicator, row_builder: nil, type_map: {}) ⇒ Matrix
Instantiate Matrix.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/honey_format/matrix/matrix.rb', line 40 def initialize( matrix, header: nil, header_converter: HoneyFormat.header_converter, header_deduplicator: HoneyFormat.config.header_deduplicator, row_builder: nil, type_map: {} ) header_row = header || matrix.shift @header = Header.new( header_row, converter: header_converter, deduplicator: header_deduplicator ) @type_map = type_map.select { |key, _v| @header.columns.include?(key) }.to_h @rows = Rows.new(matrix, columns, builder: row_builder, type_map: @type_map) end |
Instance Method Details
#columns ⇒ Array<Symbol>
Matrix columns converted from the original Matrix header
66 67 68 |
# File 'lib/honey_format/matrix/matrix.rb', line 66 def columns @header.to_a end |
#each_row {|row| ... } ⇒ Enumerator
Itereate over each row
86 87 88 89 90 |
# File 'lib/honey_format/matrix/matrix.rb', line 86 def each_row return rows.each unless block_given? rows.each { |row| yield(row) } end |
#header ⇒ Header
Original matrix header
60 61 62 |
# File 'lib/honey_format/matrix/matrix.rb', line 60 def header @header end |
#rows ⇒ Rows
Return rows
78 79 80 |
# File 'lib/honey_format/matrix/matrix.rb', line 78 def rows @rows end |
#to_csv(columns: nil) {|row| ... } ⇒ String
Convert matrix to CSV-string.
106 107 108 109 |
# File 'lib/honey_format/matrix/matrix.rb', line 106 def to_csv(columns: nil, &block) columns = columns&.map(&:to_sym) @header.to_csv(columns: columns) + @rows.to_csv(columns: columns, &block) end |
#type_map ⇒ Hash<Symbol, Symbol>
Matrix type map used
72 73 74 |
# File 'lib/honey_format/matrix/matrix.rb', line 72 def type_map @type_map end |