Class: AbstractImporter::PolymorphicMapping

Inherits:
Mapping
  • Object
show all
Defined in:
lib/abstract_importer/polymorphic_mapping.rb

Instance Attribute Summary collapse

Attributes inherited from Mapping

#depends_on

Instance Method Summary collapse

Methods inherited from Mapping

#apply!, #inspect, #to_s

Constructor Details

#initialize(collection, association) ⇒ PolymorphicMapping

Returns a new instance of PolymorphicMapping.



5
6
7
8
9
10
11
12
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 5

def initialize(collection, association)
  @collection = collection
  @foreign_key = association.foreign_key.to_sym
  @foreign_type = association.foreign_key.gsub(/_id$/, "_type").to_sym
  @table_name_by_foreign_model = Hash.new do |map, foreign_model|
    map[foreign_model] = foreign_model && foreign_model.constantize.table_name.to_sym
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



3
4
5
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 3

def collection
  @collection
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



3
4
5
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 3

def foreign_key
  @foreign_key
end

#foreign_typeObject (readonly)

Returns the value of attribute foreign_type.



3
4
5
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 3

def foreign_type
  @foreign_type
end

Instance Method Details

#applicable?(attrs) ⇒ Boolean Also known as: applies_to?

Returns:

  • (Boolean)


14
15
16
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 14

def applicable?(attrs)
  attrs.key?(foreign_key) && attrs.key?(foreign_type)
end

#apply(attrs) ⇒ Object Also known as: []



27
28
29
30
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 27

def apply(attrs)
  depends_on = foreign_table_for(attrs)
  collection.map_foreign_key(attrs[foreign_key], foreign_key, depends_on) if depends_on
end

#foreign_model_for(attrs) ⇒ Object



19
20
21
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 19

def foreign_model_for(attrs)
  attrs[foreign_type]
end

#foreign_table_for(attrs) ⇒ Object



23
24
25
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 23

def foreign_table_for(attrs)
  table_name_for(foreign_model_for(attrs))
end