Class: GtfsReader::FileRow
- Inherits:
-
Object
- Object
- GtfsReader::FileRow
- Defined in:
- lib/gtfs_reader/file_row.rb
Overview
Contains the contents of a single row read in from the file
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
Instance Method Summary collapse
-
#[](column) ⇒ Object
The parsed data for the column at this row.
-
#col?(col) ⇒ Boolean
If this row has the given column.
- #initialize(line_number, headers, data, definition, do_parse) ⇒ Array<Symbol> constructor
-
#raw(column) ⇒ Object
The data unparsed data from the column at this row.
-
#to_a ⇒ Array
An array representing this row of data.
-
#to_hash ⇒ Hash
A hash representing this row of data, where each key is the column name and each value is the parsed data for this row.
Constructor Details
#initialize(line_number, headers, data, definition, do_parse) ⇒ Array<Symbol>
13 14 15 16 17 18 19 20 |
# File 'lib/gtfs_reader/file_row.rb', line 13 def initialize(line_number, headers, data, definition, do_parse) @line_number = line_number @headers = headers @data = data @definition = definition @do_parse = do_parse @parsed = {} end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/gtfs_reader/file_row.rb', line 6 def headers @headers end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
6 7 8 |
# File 'lib/gtfs_reader/file_row.rb', line 6 def line_number @line_number end |
Instance Method Details
#[](column) ⇒ Object
Returns the parsed data for the column at this row.
25 26 27 28 29 30 31 |
# File 'lib/gtfs_reader/file_row.rb', line 25 def [](column) return raw(column) unless @do_parse @parsed[column] ||= ParserContext.new(column, self) .instance_exec(raw(column), &@definition[column].parser) end |
#col?(col) ⇒ Boolean
Returns if this row has the given column.
34 35 36 |
# File 'lib/gtfs_reader/file_row.rb', line 34 def col?(col) @headers.include?(col) end |
#raw(column) ⇒ Object
Returns the data unparsed data from the column at this row.
40 41 42 |
# File 'lib/gtfs_reader/file_row.rb', line 40 def raw(column) @data[column] end |
#to_a ⇒ Array
Returns an array representing this row of data.
53 54 55 |
# File 'lib/gtfs_reader/file_row.rb', line 53 def to_a headers.map { |h| self[h] } end |
#to_hash ⇒ Hash
Returns a hash representing this row of data, where each key is the column name and each value is the parsed data for this row.
46 47 48 49 50 |
# File 'lib/gtfs_reader/file_row.rb', line 46 def to_hash ::Hash[ *headers.inject([]) { |list, h| list << h << self[h] } ] end |