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
-
#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.
15 16 17 18 19 20 |
# File 'lib/gtfs_reader/config/column.rb', line 15 def initialize(name, opts={}, &parser) @name = name @parser = block_given? ? parser : IDENTITY_PARSER @opts = { required: false, unique: false }.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
#parser(&block) ⇒ Object
22 23 24 25 |
# File 'lib/gtfs_reader/config/column.rb', line 22 def parser(&block) @parser = block if block_given? @parser end |
#parser? ⇒ Boolean
39 40 41 |
# File 'lib/gtfs_reader/config/column.rb', line 39 def parser? parser != IDENTITY_PARSER end |
#required? ⇒ Boolean
Returns if this column is required to appear in the file.
28 29 30 |
# File 'lib/gtfs_reader/config/column.rb', line 28 def required? @opts[:required] end |
#to_s ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gtfs_reader/config/column.rb', line 43 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.
34 35 36 |
# File 'lib/gtfs_reader/config/column.rb', line 34 def unique? @opts[:unique] end |