Class: RapidTransit::Row
- Inherits:
-
Object
- Object
- RapidTransit::Row
- Defined in:
- lib/rapid_transit/row.rb
Instance Attribute Summary collapse
-
#line ⇒ Object
Returns the value of attribute line.
-
#num ⇒ Object
Returns the value of attribute num.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #[]=(key, val) ⇒ Object
- #apply(action) ⇒ Object
-
#initialize(parser, line, num) ⇒ Row
constructor
A new instance of Row.
- #input_values ⇒ Object
- #parse ⇒ Object
Constructor Details
#initialize(parser, line, num) ⇒ Row
Returns a new instance of Row.
6 7 8 9 10 11 |
# File 'lib/rapid_transit/row.rb', line 6 def initialize(parser, line, num) @parser = parser @line = line @num = num @values = input_values end |
Instance Attribute Details
#line ⇒ Object
Returns the value of attribute line.
4 5 6 |
# File 'lib/rapid_transit/row.rb', line 4 def line @line end |
#num ⇒ Object
Returns the value of attribute num.
4 5 6 |
# File 'lib/rapid_transit/row.rb', line 4 def num @num end |
#parser ⇒ Object
Returns the value of attribute parser.
4 5 6 |
# File 'lib/rapid_transit/row.rb', line 4 def parser @parser end |
#values ⇒ Object
Returns the value of attribute values.
4 5 6 |
# File 'lib/rapid_transit/row.rb', line 4 def values @values end |
Instance Method Details
#[](arg) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/rapid_transit/row.rb', line 25 def [](arg) if arg.is_a? Hash key, method = arg.first @values[key].send method else @values[arg] end end |
#[]=(key, val) ⇒ Object
34 35 36 |
# File 'lib/rapid_transit/row.rb', line 34 def []=(key, val) @values[key] = val end |
#apply(action) ⇒ Object
19 20 21 22 23 |
# File 'lib/rapid_transit/row.rb', line 19 def apply(action) return unless action.apply? self self.values[action.key] = action.result(self) check_result(action) end |
#input_values ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rapid_transit/row.rb', line 38 def input_values columns = CSV.parse_line(line, :col_sep => parser.delimiter) columns = columns.map { |c| c.strip unless c.nil? } if parser.strip unless columns.count == parser.column_names.count = "Wrong number of columns in line #{num}:\n#{line}\n" + "(Expected columns: #{parser.column_names.join(", ")})" raise RapidTransit::ColumnMismatchError, , caller end array = parser.column_names.zip(columns).map { |key, value| [key, value] } Hash[array] end |
#parse ⇒ Object
13 14 15 16 17 |
# File 'lib/rapid_transit/row.rb', line 13 def parse parser.actions.each do |action| apply action end end |