class GtfsReader::Config::Column

Defines a single column in a {FileDefinition file}.

Constants

IDENTITY_PARSER

A “parser” which simply returns its input. Used by default

Attributes

name[R]

Public Class Methods

new(name, opts = {}, &parser) click to toggle source

@param name [String] the name of the column @option opts [Boolean] :required (false) If this column is required to

appear in the given file

@option opts [Boolean] :unique (false) if values in this column need to

be unique among all rows in the file.
# 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

Public Instance Methods

parser(&block) click to toggle source
# File lib/gtfs_reader/config/column.rb, line 22
def parser(&block)
  @parser = block if block_given?
  @parser
end
parser?() click to toggle source

@return [Boolean]

# File lib/gtfs_reader/config/column.rb, line 39
def parser?
  parser != IDENTITY_PARSER
end
required?() click to toggle source

@return [Boolean] if this column is required to appear in the file

# File lib/gtfs_reader/config/column.rb, line 28
def required?
  @opts[:required]
end
to_s() click to toggle source
# 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?() click to toggle source

@return [Boolean] if values in this column need to be unique among all

rows in the file.
# File lib/gtfs_reader/config/column.rb, line 34
def unique?
  @opts[:unique]
end