Class: Trackplus::Transformation

Inherits:
Object
  • Object
show all
Defined in:
lib/trackplus/transformation.rb

Instance Method Summary collapse

Constructor Details

#initialize(mappings) ⇒ Transformation

Returns a new instance of Transformation.



5
6
7
# File 'lib/trackplus/transformation.rb', line 5

def initialize(mappings)
  @mappings = mappings || {}
end

Instance Method Details

#apply(mapping, data) ⇒ Object|nil

selects transformation strategy based on the inputs

Parameters:

  • transformation (Method|Proc|nil)

    the transformation to perform

  • data (Hash|Array|nil)

    the data to transform

Returns:

  • (Object|nil)

    results of the transformation if given, raw data otherwise



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/trackplus/transformation.rb', line 14

def apply(mapping, data)
  transformation = @mappings[mapping]
  return data unless transformation

  case data
  when nil
    data
  when Array
    data.map { |datas| transformation.call(datas) }
  else
    transformation.call(data)
  end
end