class GtfsReader::Config::FileDefinition

Describes a single file in a {FeedDefinition GTFS feed}.

Attributes

name[R]

Public Class Methods

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

@param name [String] The name of the file within the feed. @option opts [Boolean] :required (false)

If this file is required to be in the feed.
# File lib/gtfs_reader/config/file_definition.rb, line 12
def initialize(name, opts = {})
  @name = name
  @columns = {}
  @opts = { required: false }.merge(opts || {})
end

Public Instance Methods

[](name) click to toggle source

@return [Column] The column with the given name

# File lib/gtfs_reader/config/file_definition.rb, line 29
def [](name)
  @columns[name]
end
col(name, *args, &block) click to toggle source

Creates a column with the given name.

@param name [String] The name of the column to define. @param args [Array] The first element of this args list is used as a

+Hash+ of options to create the new column with.

@param block [Proc] An optional block used to parse the values of this

column on each row.

@yieldparam input [String] The value of this column for a particular row. @yieldreturn Any kind of object. @return [Column] The newly created column.

# 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() click to toggle source
# File lib/gtfs_reader/config/file_definition.rb, line 33
def columns
  @columns.values
end
filename() click to toggle source

@return [String] The filename of this file within the GTFS feed.

# File lib/gtfs_reader/config/file_definition.rb, line 24
def filename
  "#{name}.txt"
end
optional_columns() click to toggle source

@return [Array<Column>] The columns not required to appear in this file.

# File lib/gtfs_reader/config/file_definition.rb, line 43
def optional_columns
  columns.reject(&:required?)
end
output_map(reverse_map, default = nil) click to toggle source

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.

@param reverse_map [Hash] A map of returns values to their input values.

This is in reverse because it looks better, like a list of labels:
+{bus: 3, ferry: 4}+

@param default [] The value to return if there is no mapping for a given

input.
# 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?() click to toggle source

@return [Boolean] If this file is required to be in the feed.

# File lib/gtfs_reader/config/file_definition.rb, line 19
def required?
  @opts[:required]
end
required_columns() click to toggle source

@return [Array<Column>] The columns required to appear in this file.

# File lib/gtfs_reader/config/file_definition.rb, line 38
def required_columns
  columns.select(&:required?)
end
unique_columns() click to toggle source

@return [Array<Column>] The columns which cannot have two rows with the

same value.
# File lib/gtfs_reader/config/file_definition.rb, line 49
def unique_columns
  columns.select(&:unique?)
end