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

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/gtfs_reader/file_row.rb', line 6

def headers
  @headers
end

#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:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • column (Symbol)

    the name of the column to fetch

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_aArray

Returns an array representing this row of data.

Returns:

  • (Array)

    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_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



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