Class: GtfsReader::Config::Column
- Inherits:
-
Object
- Object
- GtfsReader::Config::Column
- Defined in:
- lib/gtfs_reader/config/column.rb
Overview
Defines a single column in a file.
Constant Summary collapse
- IDENTITY_PARSER =
A “parser” which simply returns its input. Used by default
->(arg) { arg }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#alias ⇒ String?
This column’s name’s alias, if there is one.
-
#initialize(name, opts = {}, &parser) ⇒ Column
constructor
A new instance of Column.
- #parser(&block) ⇒ Object
- #parser? ⇒ Boolean
-
#required? ⇒ Boolean
If this column is required to appear in the file.
- #to_s ⇒ Object
-
#unique? ⇒ Boolean
If values in this column need to be unique among all rows in the file.
Constructor Details
#initialize(name, opts = {}, &parser) ⇒ Column
Returns a new instance of Column.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gtfs_reader/config/column.rb', line 18 def initialize(name, opts={}, &parser) @name = name @parser = block_given? ? parser : IDENTITY_PARSER @opts = { required: false, unique: false, alias: nil }.merge (opts || {}) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/gtfs_reader/config/column.rb', line 8 def name @name end |
Instance Method Details
#alias ⇒ String?
Returns this column’s name’s alias, if there is one.
46 47 48 |
# File 'lib/gtfs_reader/config/column.rb', line 46 def alias @opts[:alias] end |
#parser(&block) ⇒ Object
29 30 31 32 |
# File 'lib/gtfs_reader/config/column.rb', line 29 def parser(&block) @parser = block if block_given? @parser end |
#parser? ⇒ Boolean
51 52 53 |
# File 'lib/gtfs_reader/config/column.rb', line 51 def parser? parser != IDENTITY_PARSER end |
#required? ⇒ Boolean
Returns if this column is required to appear in the file.
35 36 37 |
# File 'lib/gtfs_reader/config/column.rb', line 35 def required? @opts[:required] end |
#to_s ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gtfs_reader/config/column.rb', line 55 def to_s opts = @opts.map do |key,value| case value when true then key when false,nil then nil else "#{key}=#{value}" end end.reject &:nil? opts << 'has_parser' if parser? str = name.to_s str << ": #{opts.join ', '}" unless opts.empty? "[#{str}]" end |
#unique? ⇒ Boolean
Returns if values in this column need to be unique among all rows in the file.
41 42 43 |
# File 'lib/gtfs_reader/config/column.rb', line 41 def unique? @opts[:unique] end |