Class: RapidTransit::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid_transit/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, line, num) ⇒ Row

Returns a new instance of Row.



6
7
8
9
10
11
# File 'lib/rapid_transit/row.rb', line 6

def initialize(parser, line, num)
  @parser = parser
  @line = line
  @num = num
  @values = input_values
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



4
5
6
# File 'lib/rapid_transit/row.rb', line 4

def line
  @line
end

#numObject

Returns the value of attribute num.



4
5
6
# File 'lib/rapid_transit/row.rb', line 4

def num
  @num
end

#parserObject

Returns the value of attribute parser.



4
5
6
# File 'lib/rapid_transit/row.rb', line 4

def parser
  @parser
end

#valuesObject

Returns the value of attribute values.



4
5
6
# File 'lib/rapid_transit/row.rb', line 4

def values
  @values
end

Instance Method Details

#[](arg) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rapid_transit/row.rb', line 25

def [](arg)
  if arg.is_a? Hash
    key, method = arg.first
    @values[key].send method
  else
    @values[arg]
  end
end

#[]=(key, val) ⇒ Object



34
35
36
# File 'lib/rapid_transit/row.rb', line 34

def []=(key, val)
  @values[key] = val
end

#apply(action) ⇒ Object



19
20
21
22
23
# File 'lib/rapid_transit/row.rb', line 19

def apply(action)
  return unless action.apply? self
  self.values[action.key] = action.result(self)
  check_result(action)
end

#input_valuesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rapid_transit/row.rb', line 38

def input_values
  columns = CSV.parse_line(line, :col_sep => parser.delimiter)
  columns = columns.map { |c| c.strip unless c.nil? } if parser.strip
  unless columns.count == parser.column_names.count
    message = "Wrong number of columns in line #{num}:\n#{line}\n" +
              "(Expected columns: #{parser.column_names.join(", ")})"
    raise RapidTransit::ColumnMismatchError, message, caller
  end
  array = parser.column_names.zip(columns).map { |key, value| [key, value] }
  Hash[array]
end

#parseObject



13
14
15
16
17
# File 'lib/rapid_transit/row.rb', line 13

def parse
  parser.actions.each do |action|
    apply action
  end
end