Class: GtfsReader::FileRow

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(line_number, headers, data, definition, do_parse) ⇒ Array<Symbol>

Parameters:

  • line_number (Integer)

    the line number from the source file

  • data (CSV::Row)

    the data for this row

  • definition (FileDefinition)

    the definition of the columns that the data in this row represent



13
14
15
16
17
# File 'lib/gtfs_reader/file_row.rb', line 13

def initialize(line_number, headers, data, definition, do_parse)
  @line_number, @headers, @data, @definition, @do_parse =
      line_number, headers, data, definition, do_parse
  @parsed = {}
end

Instance Attribute Details

#line_numberObject (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.

Parameters:

  • column (Symbol)

    the name of the column to fetch

Returns:

  • the parsed data for the column at this row

See Also:



27
28
29
30
31
32
33
34
# File 'lib/gtfs_reader/file_row.rb', line 27

def [](column)
  return raw(column) unless @do_parse

  @parsed[column] ||= begin
    ParserContext.new(column, self).
        instance_exec raw(column), &@definition[column].parser
  end
end

#headersArray<Symbol>

Returns:

  • (Array<Symbol>)


20
21
22
# File 'lib/gtfs_reader/file_row.rb', line 20

def headers
  @headers
end

#raw(column) ⇒ Object

Returns the data unparsed data from the column at this row.

Parameters:

  • column (Symbol)

    the name of the column to fetch

Returns:

  • the data unparsed data from the column at this row



38
39
40
# File 'lib/gtfs_reader/file_row.rb', line 38

def raw(column)
  @data[column]
end

#to_aArray

Returns an array representing this row of data.

Returns:

  • (Array)

    an array representing this row of data



49
50
51
# File 'lib/gtfs_reader/file_row.rb', line 49

def to_a
  headers.map {|h| self[h] }
end

#to_hashHash

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.

Returns:

  • (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



44
45
46
# File 'lib/gtfs_reader/file_row.rb', line 44

def to_hash
  ::Hash[ *headers.inject([]) {|list,h| list << h << self[h] } ]
end