Class: Etna::CsvImporter::NestedRowProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/csvs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, lineno, context) ⇒ NestedRowProcessor

Returns a new instance of NestedRowProcessor.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/etna/csvs.rb', line 59

def initialize(row, lineno, context)
  @row = row
  @lineno = lineno
  @context = context
  @errors = []

  # If a parent context changes, all child contexts are invalidated.  But since parent contexts are changed
  # before the relationship of child contexts are declared, we have to track that so that when a child context
  # dependency is declared we can clear it based on wether parents have changed.
  @changed = {}
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



57
58
59
# File 'lib/etna/csvs.rb', line 57

def context
  @context
end

#errorsObject (readonly)

Returns the value of attribute errors.



57
58
59
# File 'lib/etna/csvs.rb', line 57

def errors
  @errors
end

#linenoObject (readonly)

Returns the value of attribute lineno.



57
58
59
# File 'lib/etna/csvs.rb', line 57

def lineno
  @lineno
end

#rowObject (readonly)

Returns the value of attribute row.



57
58
59
# File 'lib/etna/csvs.rb', line 57

def row
  @row
end

Instance Method Details

#process(column, *parents, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/etna/csvs.rb', line 71

def process(column, *parents, &block)
  if parents.any? { |p| @changed.include?(p) }
    @changed[column] = true
    @context[column] = nil
  end

  return self if (next_val = row[column]).nil?
  @changed[column] = true

  parent_values = parents.map do |p|
    if @context[p].nil?
      raise ImportError.new("Found a #{column} value, but no previous #{p} had been given!", @lineno)
    end

    @context[p]
  end

  begin
    next_val = yield next_val, *parent_values, self if block_given?
  rescue ImportError => e
    e.lineno = @lineno
    raise e
  end

  @context[column] = next_val
  self
end