Class: Preserves::Mapper::HasMany

Inherits:
Relation
  • Object
show all
Defined in:
lib/preserves/mapper/has_many.rb

Instance Attribute Summary

Attributes inherited from Relation

#mapping, #object, #record, #relation_name, #relation_result_set

Instance Method Summary collapse

Methods inherited from Relation

#assign_attribute, #initialize, #relation_repo

Constructor Details

This class inherits a constructor from Preserves::Mapper::Relation

Instance Method Details

#map!Object



7
8
9
# File 'lib/preserves/mapper/has_many.rb', line 7

def map!
  assign_attribute(object, relation_name, relation_repo.map(relation_results_for_this_object))
end

#relation_foreign_keyObject



15
16
17
# File 'lib/preserves/mapper/has_many.rb', line 15

def relation_foreign_key
  @relation_foreign_key ||= relation_settings.fetch(:foreign_key) { "#{mapping.model_class.to_s.downcase}_id" }
end

#relation_results_for_this_objectObject



11
12
13
# File 'lib/preserves/mapper/has_many.rb', line 11

def relation_results_for_this_object
  @relation_results_for_this_object ||= relation_result_set.select{ |r| r[relation_foreign_key] == record.fetch(mapping.primary_key) }
end

#relation_settingsObject



19
20
21
# File 'lib/preserves/mapper/has_many.rb', line 19

def relation_settings
  @relation_settings ||= mapping.has_many_mappings.fetch(relation_name)
end