Class: Workable::Transformation

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

Instance Method Summary collapse

Constructor Details

#initialize(mappings) ⇒ Transformation

Returns a new instance of Transformation.



3
4
5
# File 'lib/workable/transformation.rb', line 3

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



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/workable/transformation.rb', line 12

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