Class: GtfsReader::Config::FileDefinition
- Inherits:
-
Object
- Object
- GtfsReader::Config::FileDefinition
- Defined in:
- lib/gtfs_reader/config/file_definition.rb
Overview
Describes a single file in a GTFS feed.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#[](name) ⇒ Column
The column with the given name.
-
#col(name, *args, &block) {|input| ... } ⇒ Column
Creates a column with the given name.
- #columns ⇒ Object
-
#filename ⇒ String
The filename of this file within the GTFS feed.
-
#initialize(name, opts = {}) ⇒ FileDefinition
constructor
A new instance of FileDefinition.
-
#optional_columns ⇒ Array<Column>
The columns not required to appear in this file.
-
#output_map(reverse_map, default = nil) ⇒ Object
Creates an input-output proc to convert column values from one form to another.
-
#required? ⇒ Boolean
If this file is required to be in the feed.
-
#required_columns ⇒ Array<Column>
The columns required to appear in this file.
-
#unique_columns ⇒ Array<Column>
The columns which cannot have two rows with the same value.
Constructor Details
#initialize(name, opts = {}) ⇒ FileDefinition
Returns a new instance of FileDefinition.
12 13 14 15 16 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 12 def initialize(name, opts = {}) @name = name @columns = {} @opts = { required: false }.merge(opts || {}) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 7 def name @name end |
Instance Method Details
#[](name) ⇒ Column
Returns The column with the given name.
29 30 31 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 29 def [](name) @columns[name] end |
#col(name, *args, &block) {|input| ... } ⇒ Column
Creates a column with the given name.
63 64 65 66 67 68 69 70 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 63 def col(name, *args, &block) if @columns.key? name @columns[name].parser(&block) if block_given? return @columns[name] end @columns[name] = Column.new(name, args.first, &block) end |
#columns ⇒ Object
33 34 35 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 33 def columns @columns.values end |
#filename ⇒ String
Returns The filename of this file within the GTFS feed.
24 25 26 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 24 def filename "#{name}.txt" end |
#optional_columns ⇒ Array<Column>
Returns The columns not required to appear in this file.
43 44 45 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 43 def optional_columns columns.reject(&:required?) end |
#output_map(reverse_map, default = nil) ⇒ Object
Creates an input-output proc to convert column values from one form to another.
Many parser procs simply map a set of known values to another set of known values. This helper creates such a proc from a given hash and optional default.
84 85 86 87 88 89 90 91 92 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 84 def output_map(reverse_map, default = nil) if reverse_map.values.uniq.length != reverse_map.values.length raise FileDefinitionError, "Duplicate values given: #{reverse_map}" end map = default.nil? ? {} : Hash.new(default) reverse_map.each { |k, v| map[v] = k } map.method(:[]).to_proc end |
#required? ⇒ Boolean
Returns If this file is required to be in the feed.
19 20 21 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 19 def required? @opts[:required] end |
#required_columns ⇒ Array<Column>
Returns The columns required to appear in this file.
38 39 40 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 38 def required_columns columns.select(&:required?) end |
#unique_columns ⇒ Array<Column>
Returns The columns which cannot have two rows with the same value.
49 50 51 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 49 def unique_columns columns.select(&:unique?) end |