class GtfsReader::FileRow

Contains the contents of a single row read in from the file

Attributes

headers[R]
line_number[R]

Public Class Methods

new(line_number, headers, data, definition, do_parse) click to toggle source

@param line_number [Integer] the line number from the source file @return [Array<Symbol>] @param data [CSV::Row] the data for this row @param definition [FileDefinition] the definition of the columns that the

data in this row represent
# 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

Public Instance Methods

[](column) click to toggle source

@param column [Symbol] the name of the column to fetch @return the parsed data for the column at this row @see raw

# 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) click to toggle source

@return [Boolean] if this row has the given column

# File lib/gtfs_reader/file_row.rb, line 34
def col?(col)
  @headers.include?(col)
end
raw(column) click to toggle source

@param (see []) @return the data unparsed data from the column at this row

# File lib/gtfs_reader/file_row.rb, line 40
def raw(column)
  @data[column]
end
to_a() click to toggle source

@return [Array] an array representing this row of data

# File lib/gtfs_reader/file_row.rb, line 53
def to_a
  headers.map { |h| self[h] }
end
to_hash() click to toggle source

@return [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
# File lib/gtfs_reader/file_row.rb, line 46
def to_hash
  ::Hash[
    *headers.inject([]) { |list, h| list << h << self[h] }
  ]
end