Class: Philiprehberger::CsvKit::Row
- Inherits:
-
Object
- Object
- Philiprehberger::CsvKit::Row
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/csv_kit/row.rb
Overview
Wraps a CSV row as a hash with symbolized keys.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access a value by symbolized key.
-
#[]=(key, value) ⇒ Object
Set a value by symbolized key.
-
#delete(key) ⇒ Object?
Delete a key from the row.
-
#each {|Symbol, Object| ... } ⇒ Object
Iterate over key-value pairs.
-
#initialize(data) ⇒ Row
constructor
A new instance of Row.
-
#key?(key) ⇒ Boolean
Check if a key exists.
-
#keys ⇒ Array<Symbol>
Return column names.
-
#merge(other) ⇒ Row
Merge another hash or Row into this row, returning a new Row.
-
#size ⇒ Integer
Return the number of columns.
-
#to_h ⇒ Hash{Symbol => Object}
Return the row as a plain hash.
-
#values ⇒ Array<Object>
Return column values.
Constructor Details
#initialize(data) ⇒ Row
Returns a new instance of Row.
10 11 12 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 10 def initialize(data) @data = data end |
Instance Method Details
#[](key) ⇒ Object
Access a value by symbolized key.
55 56 57 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 55 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Set a value by symbolized key.
63 64 65 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 63 def []=(key, value) @data[key] = value end |
#delete(key) ⇒ Object?
Delete a key from the row.
79 80 81 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 79 def delete(key) @data.delete(key) end |
#each {|Symbol, Object| ... } ⇒ Object
Iterate over key-value pairs.
17 18 19 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 17 def each(&) @data.each(&) end |
#key?(key) ⇒ Boolean
Check if a key exists.
71 72 73 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 71 def key?(key) @data.key?(key) end |
#keys ⇒ Array<Symbol>
Return column names.
24 25 26 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 24 def keys @data.keys end |
#merge(other) ⇒ Row
Merge another hash or Row into this row, returning a new Row.
46 47 48 49 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 46 def merge(other) other_data = other.is_a?(Row) ? other.to_h : other Row.new(@data.merge(other_data)) end |
#size ⇒ Integer
Return the number of columns.
38 39 40 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 38 def size @data.size end |
#to_h ⇒ Hash{Symbol => Object}
Return the row as a plain hash.
86 87 88 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 86 def to_h @data.dup end |
#values ⇒ Array<Object>
Return column values.
31 32 33 |
# File 'lib/philiprehberger/csv_kit/row.rb', line 31 def values @data.values end |