Class: Preserves::Mapper::BelongsTo

Inherits:
Relation
  • Object
show all
Defined in:
lib/preserves/mapper/belongs_to.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/belongs_to.rb', line 7

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

#relation_foreign_keyObject



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

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

#relation_result_for_this_objectObject



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

def relation_result_for_this_object
  @relation_result_for_this_object ||= relation_result_set.find{ |r| r[relation_repo.mapping.primary_key] == record.fetch(relation_foreign_key) }
end

#relation_settingsObject



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

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