Class: Tablecloth::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/tablecloth/transformer.rb

Instance Method Summary collapse

Constructor Details

#initialize(definition, *args) ⇒ Transformer

Returns a new instance of Transformer.



5
6
7
8
# File 'lib/tablecloth/transformer.rb', line 5

def initialize(definition, *args)
  @definition = definition
  @args = args
end

Instance Method Details

#call(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tablecloth/transformer.rb', line 10

def call(value)
  previous_values = {}

  value.map do |row|
    row.each_with_object({}) do |(key, value), hash|
      column = @definition[key]
      previous_value = previous_values[key]
      result = if previous_value && value == ""
        previous_value
      else
        visit(column).call(value)
      end

      previous_values[key] = result if column.retain_previous_value?
      hash[key] = result
    end
  end
end