Class: AnixeCsv::Row
- Inherits:
-
Object
- Object
- AnixeCsv::Row
- Includes:
- Enumerable
- Defined in:
- lib/anixe_csv/row.rb
Overview
A class returned by Reader, a wrapper around an array allowing to access fields by their names from the header of the file
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](index) ⇒ Object
- #each(*args) ⇒ Object
-
#initialize(row, fields) ⇒ Row
constructor
A new instance of Row.
-
#method_missing(m, *args) ⇒ Object
private :row, :fields.
- #values ⇒ Object
Constructor Details
#initialize(row, fields) ⇒ Row
Returns a new instance of Row.
5 6 7 8 9 10 |
# File 'lib/anixe_csv/row.rb', line 5 def initialize(row, fields) throw ArgumentError unless row.kind_of? Array throw ArgumentError unless fields.kind_of? Hash @row = row @fields = fields end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
private :row, :fields
15 16 17 18 |
# File 'lib/anixe_csv/row.rb', line 15 def method_missing(m, *args) throw ArgumentError, "#{m} is not in the headers" unless @fields.keys.include? m @row[@fields[m]] end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
12 13 14 |
# File 'lib/anixe_csv/row.rb', line 12 def fields @fields end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
12 13 14 |
# File 'lib/anixe_csv/row.rb', line 12 def row @row end |
Instance Method Details
#+(other) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/anixe_csv/row.rb', line 28 def +(other) new_row = (self.row + other.row) header = merge_headers(self.fields, other.fields) new_fields = Hash[ header.collect {|v| [v, header.index(v)] } ] Row.new(new_row, new_fields) end |
#[](index) ⇒ Object
20 21 22 |
# File 'lib/anixe_csv/row.rb', line 20 def [](index) @row[index] end |
#each(*args) ⇒ Object
24 25 26 |
# File 'lib/anixe_csv/row.rb', line 24 def each(*args) @row.each(*args) {|i| yield i} end |
#values ⇒ Object
37 38 39 |
# File 'lib/anixe_csv/row.rb', line 37 def values @row end |