Class: Migratrix::Transforms::Map

Inherits:
Transform show all
Defined in:
lib/migratrix/transforms/map.rb

Overview

Map is a transform that maps attributes from the source object to the target object.

:transform: a hash with dst => src keys, where dst is an attribute on the transformed target object and src is either an attribute on the source object or a Proc that receives the entire extracted row and returns a value to be set.

TODO: Right now map makes a lot of hard-coded assumptions as a result of the primary test case. Notably that target is a Hash, final class is a Hash keyed by transformed_object, etc.

TODO: Figure out how to do both of these strategies with Map:

# Create object and then modify it sequentially new_object = target.new map.each do |dst, src|

new_object[dst] = extracted_item[src]

end

# Build up creation params and then new the object hash = Hash.new map.each do [dst, src]

hash[dst] = extracted_item[src]

end new_object = target.new(hash)

Instance Attribute Summary collapse

Attributes inherited from Transform

#extraction, #name, #options, #transformations

Instance Method Summary collapse

Methods inherited from Transform

#finalize_object, #transform

Constructor Details

#initialize(name, options = {}) ⇒ Map

Returns a new instance of Map.



32
33
34
# File 'lib/migratrix/transforms/map.rb', line 32

def initialize(name, options={})
  super
end

Instance Attribute Details

#mapObject

Returns the value of attribute map.



30
31
32
# File 'lib/migratrix/transforms/map.rb', line 30

def map
  @map
end

Instance Method Details

#apply_attribute(object, attribute_or_apply, value) ⇒ Object



44
45
46
# File 'lib/migratrix/transforms/map.rb', line 44

def apply_attribute(object, attribute_or_apply, value)
  object[attribute_or_apply] = value
end

#create_new_object(extracted_row) ⇒ Object



40
41
42
# File 'lib/migratrix/transforms/map.rb', line 40

def create_new_object(extracted_row)
  Hash.new
end

#create_transformed_collectionObject



36
37
38
# File 'lib/migratrix/transforms/map.rb', line 36

def create_transformed_collection
  Hash.new
end

#extract_attribute(object, attribute_or_extract) ⇒ Object



48
49
50
# File 'lib/migratrix/transforms/map.rb', line 48

def extract_attribute(object, attribute_or_extract)
  object[attribute_or_extract]
end

#store_transformed_object(object, collection) ⇒ Object



52
53
54
# File 'lib/migratrix/transforms/map.rb', line 52

def store_transformed_object(object, collection)
  collection[object[:id]] = object
end