Class: AbstractImporter::PolymorphicMapping
- Defined in:
- lib/abstract_importer/polymorphic_mapping.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#foreign_type ⇒ Object
readonly
Returns the value of attribute foreign_type.
Attributes inherited from Mapping
Instance Method Summary collapse
- #applicable?(attrs) ⇒ Boolean (also: #applies_to?)
- #apply(attrs) ⇒ Object (also: #[])
- #foreign_model_for(attrs) ⇒ Object
- #foreign_table_for(attrs) ⇒ Object
-
#initialize(collection, association) ⇒ PolymorphicMapping
constructor
A new instance of PolymorphicMapping.
Methods inherited from Mapping
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
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
3 4 5 |
# File 'lib/abstract_importer/polymorphic_mapping.rb', line 3 def collection @collection end |
#foreign_key ⇒ Object (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_type ⇒ Object (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?
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 |