Class: GtfsReader::FileReader
- Inherits:
-
Object
- Object
- GtfsReader::FileReader
- Includes:
- Enumerable
- Defined in:
- lib/gtfs_reader/file_reader.rb
Overview
Iterates over the rows in a single file using a provided definition.
Instance Attribute Summary collapse
-
#col_names ⇒ Object
readonly
Returns the value of attribute col_names.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Instance Method Summary collapse
- #each ⇒ Object
- #filename ⇒ Object
-
#initialize(data, definition, opts = {}) ⇒ FileReader
constructor
A new instance of FileReader.
-
#shift ⇒ FileRow?
The next row from the file, or
nil
if the end of the file has been reached.
Constructor Details
#initialize(data, definition, opts = {}) ⇒ FileReader
Returns a new instance of FileReader.
20 21 22 23 24 25 26 27 28 29 |
# 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, @do_parse, @return_hash = definition, opts[:parse], opts[:hash] @index = 0 @csv_headers = @csv.shift.headers @columns = find_columns opts[:validate] end |
Instance Attribute Details
#col_names ⇒ Object (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 |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
15 16 17 |
# File 'lib/gtfs_reader/file_reader.rb', line 15 def columns @columns end |
#definition ⇒ Object (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 #each ⇒ Enumerator
41 42 43 44 45 46 47 |
# File 'lib/gtfs_reader/file_reader.rb', line 41 def each return to_enum :each unless block_given? while row = shift yield(@return_hash ? row.to_hash : row.to_a) end end |
#filename ⇒ Object
31 32 33 |
# File 'lib/gtfs_reader/file_reader.rb', line 31 def filename @definition.filename end |
#shift ⇒ FileRow?
Returns the next row from the file, or nil
if the end of the file has been reached.
51 52 53 54 55 |
# File 'lib/gtfs_reader/file_reader.rb', line 51 def shift if row = @csv.shift file_row(row).tap { @index += 1 } end end |