Class: GtfsReader::FileReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gtfs_reader/file_reader.rb

Overview

Iterates over the rows in a single file using a provided definition.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, definition, opts = {}) ⇒ FileReader

Returns a new instance of FileReader.

Parameters:

  • data (IO, String)

    CSV data

  • definition (FileDefinition)

    describes the expected columns in this file



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gtfs_reader/file_reader.rb', line 20

def initialize(data, definition, opts = {})
  opts = { parse: true, validate: false, hash: true }.merge(opts)

  @csv = CSV.new(data, CSV_OPTIONS)
  @definition = definition
  @do_parse = opts[:parse]
  @return_hash = opts[:hash]
  @index = 0
  @csv_headers = @csv.shift.headers
  @columns = find_columns(opts[:validate])
end

Instance Attribute Details

#col_namesObject (readonly)

Returns the value of attribute col_names.



15
16
17
# File 'lib/gtfs_reader/file_reader.rb', line 15

def col_names
  @col_names
end

#columnsObject (readonly)

Returns the value of attribute columns.



15
16
17
# File 'lib/gtfs_reader/file_reader.rb', line 15

def columns
  @columns
end

#definitionObject (readonly)

Returns the value of attribute definition.



15
16
17
# File 'lib/gtfs_reader/file_reader.rb', line 15

def definition
  @definition
end

Instance Method Details

#each {|hash| ... } ⇒ Object #eachEnumerator

Overloads:

  • #each {|hash| ... } ⇒ Object

    Yield Parameters:

    • hash (Hash)

      a hash of columns to their values in this row

  • #eachEnumerator

    Returns an Enumerator that iterates of the rows in the file.

    Returns:

    • (Enumerator)

      an Enumerator that iterates of the rows in the file

See Also:



42
43
44
45
46
47
48
# File 'lib/gtfs_reader/file_reader.rb', line 42

def each
  return to_enum(:each) unless block_given?

  while (row = shift)
    yield(@return_hash ? row.to_hash : row.to_a)
  end
end

#filenameObject



32
33
34
# File 'lib/gtfs_reader/file_reader.rb', line 32

def filename
  @definition.filename
end

#shiftFileRow?

Returns the next row from the file, or nil if the end of the file has been reached.

Returns:

  • (FileRow, nil)

    the next row from the file, or nil if the end of the file has been reached.



52
53
54
55
# File 'lib/gtfs_reader/file_reader.rb', line 52

def shift
  row = @csv.shift
  file_row(row).tap { @index += 1 } if row
end