Class: Preserves::Mapper::BelongsTo
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
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_key ⇒ Object
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_object ⇒ Object
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_settings ⇒ Object
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
|