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(default = nil, reverse_map) ⇒ Object
Creates an input-output proc to convert column values from one form to another.
-
#prefix(sym, &blk) ⇒ Object
Starts a new block within which any defined columns will have the given
symprefixed to its name (joined with an underscore). -
#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.
13 14 15 16 17 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 13 def initialize(name, opts={}) @name, @columns = name, {} @opts = { required: false }.merge (opts || {}) @aliases = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 8 def name @name end |
Instance Method Details
#[](name) ⇒ Column
Returns The column with the given name.
30 31 32 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 30 def [](name) @columns[name] end |
#col(name, *args, &block) {|input| ... } ⇒ Column
Creates a column with the given name.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 64 def col(name, *args, &block) name = @aliases[name] if @aliases.key? name if @columns.key? name @columns[name].parser &block if block_given? return @columns[name] end (@columns[name] = Column.new name, args.first, &block).tap do |col| @aliases[col.alias] = name if col.alias end end |
#columns ⇒ Object
34 35 36 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 34 def columns @columns.values end |
#filename ⇒ String
Returns The filename of this file within the GTFS feed.
25 26 27 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 25 def filename "#{name}.txt" end |
#optional_columns ⇒ Array<Column>
Returns The columns not required to appear in this file.
44 45 46 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 44 def optional_columns columns.reject &:required? end |
#output_map(default = nil, reverse_map) ⇒ 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.
100 101 102 103 104 105 106 107 108 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 100 def output_map(default=nil, reverse_map) 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 |
#prefix(sym, &blk) ⇒ Object
Starts a new block within which any defined columns will have the given sym prefixed to its name (joined with an underscore). Also, the defined name given within the block will be aliased to the column.
84 85 86 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 84 def prefix(sym, &blk) PrefixedColumnSetter.new(self, sym.to_s).instance_exec &blk end |
#required? ⇒ Boolean
Returns If this file is required to be in the feed.
20 21 22 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 20 def required? @opts[:required] end |
#required_columns ⇒ Array<Column>
Returns The columns required to appear in this file.
39 40 41 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 39 def required_columns columns.select &:required? end |
#unique_columns ⇒ Array<Column>
Returns The columns which cannot have two rows with the same value.
50 51 52 |
# File 'lib/gtfs_reader/config/file_definition.rb', line 50 def unique_columns columns.select &:unique? end |