Class: GtfsReader::Config::Column

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, &parser) ⇒ Column

Returns a new instance of Column.

Parameters:

  • name (String)

    the name of the column

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

    a customizable set of options

Options Hash (opts):

  • :required (Boolean) — default: false

    If this column is required to appear in the given file

  • :alias (String)

    an alternative name for this column. Many column names are needlessly prefixed with their filename: Stop.stop_name could be aliased to Stop.name for example.

  • :unique (Boolean) — default: false

    if values in this column need to be unique among all rows in the file.



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

#nameObject (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

#aliasString?

Returns this column’s name’s alias, if there is one.

Returns:

  • (String, nil)

    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

Returns:

  • (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.

Returns:

  • (Boolean)

    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_sObject



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.

Returns:

  • (Boolean)

    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