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

  • :unique (Boolean) — default: false

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



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

#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

#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

Returns:

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

Returns:

  • (Boolean)

    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_sObject



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.

Returns:

  • (Boolean)

    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