Class: GtfsReader::Config::FileDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/gtfs_reader/config/file_definition.rb

Overview

Describes a single file in a GTFS feed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ FileDefinition

Returns a new instance of FileDefinition.

Parameters:

  • name (String)

    The name of the file within the feed.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :required (Boolean) — default: false

    If this file is required to be in the feed.



12
13
14
15
# File 'lib/gtfs_reader/config/file_definition.rb', line 12

def initialize(name, opts={})
  @name, @columns = name, {}
  @opts = { required: false }.merge (opts || {})
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/gtfs_reader/config/file_definition.rb', line 7

def name
  @name
end

Instance Method Details

#[](name) ⇒ Column

Returns The column with the given name.

Returns:

  • (Column)

    The column with the given name



28
29
30
# File 'lib/gtfs_reader/config/file_definition.rb', line 28

def [](name)
  @columns[name]
end

#col(name, *args, &block) {|input| ... } ⇒ Column

Creates a column with the given name.

Parameters:

  • name (String)

    The name of the column to define.

  • args (Array)

    The first element of this args list is used as a Hash of options to create the new column with.

  • block (Proc)

    An optional block used to parse the values of this column on each row.

Yield Parameters:

  • input (String)

    The value of this column for a particular row.

Yield Returns:

  • Any kind of object.

Returns:

  • (Column)

    The newly created column.



62
63
64
65
66
67
68
69
# File 'lib/gtfs_reader/config/file_definition.rb', line 62

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

#columnsObject



32
33
34
# File 'lib/gtfs_reader/config/file_definition.rb', line 32

def columns
  @columns.values
end

#filenameString

Returns The filename of this file within the GTFS feed.

Returns:

  • (String)

    The filename of this file within the GTFS feed.



23
24
25
# File 'lib/gtfs_reader/config/file_definition.rb', line 23

def filename
  "#{name}.txt"
end

#optional_columnsArray<Column>

Returns The columns not required to appear in this file.

Returns:

  • (Array<Column>)

    The columns not required to appear in this file.



42
43
44
# File 'lib/gtfs_reader/config/file_definition.rb', line 42

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.

Parameters:

  • default (defaults to: nil)

    The value to return if there is no mapping for a given

    input.

  • 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}



83
84
85
86
87
88
89
90
91
# File 'lib/gtfs_reader/config/file_definition.rb', line 83

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

#required?Boolean

Returns If this file is required to be in the feed.

Returns:

  • (Boolean)

    If this file is required to be in the feed.



18
19
20
# File 'lib/gtfs_reader/config/file_definition.rb', line 18

def required?
  @opts[:required]
end

#required_columnsArray<Column>

Returns The columns required to appear in this file.

Returns:

  • (Array<Column>)

    The columns required to appear in this file.



37
38
39
# File 'lib/gtfs_reader/config/file_definition.rb', line 37

def required_columns
  columns.select &:required?
end

#unique_columnsArray<Column>

Returns The columns which cannot have two rows with the same value.

Returns:

  • (Array<Column>)

    The columns which cannot have two rows with the same value.



48
49
50
# File 'lib/gtfs_reader/config/file_definition.rb', line 48

def unique_columns
  columns.select &:unique?
end