Class: SequelMapper::ManyToOneAssociation
- Inherits:
-
Object
- Object
- SequelMapper::ManyToOneAssociation
- Defined in:
- lib/sequel_mapper/many_to_one_association.rb
Instance Attribute Summary collapse
-
#mapping_name ⇒ Object
readonly
Returns the value of attribute mapping_name.
Instance Method Summary collapse
- #build_proxy(data_superset:, loader:, record:) ⇒ Object
- #build_query(superset, record) ⇒ Object
- #dump(parent_record, collection, &block) ⇒ Object (also: #delete)
- #eager_superset(superset, associated_dataset) ⇒ Object
-
#initialize(mapping_name:, foreign_key:, key:, proxy_factory:) ⇒ ManyToOneAssociation
constructor
A new instance of ManyToOneAssociation.
Constructor Details
#initialize(mapping_name:, foreign_key:, key:, proxy_factory:) ⇒ ManyToOneAssociation
Returns a new instance of ManyToOneAssociation.
5 6 7 8 9 10 |
# File 'lib/sequel_mapper/many_to_one_association.rb', line 5 def initialize(mapping_name:, foreign_key:, key:, proxy_factory:) @mapping_name = mapping_name @foreign_key = foreign_key @key = key @proxy_factory = proxy_factory end |
Instance Attribute Details
#mapping_name ⇒ Object (readonly)
Returns the value of attribute mapping_name.
12 13 14 |
# File 'lib/sequel_mapper/many_to_one_association.rb', line 12 def mapping_name @mapping_name end |
Instance Method Details
#build_proxy(data_superset:, loader:, record:) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/sequel_mapper/many_to_one_association.rb', line 17 def build_proxy(data_superset:, loader:, record:) proxy_factory.call( query: build_query(data_superset, record), loader: loader, preloaded_data: { key => foreign_key_value(record), }, ) end |
#build_query(superset, record) ⇒ Object
33 34 35 |
# File 'lib/sequel_mapper/many_to_one_association.rb', line 33 def build_query(superset, record) superset.where(key => foreign_key_value(record)) end |
#dump(parent_record, collection, &block) ⇒ Object Also known as: delete
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sequel_mapper/many_to_one_association.rb', line 37 def dump(parent_record, collection, &block) collection.flat_map { |object| block.call(mapping_name, object, _foreign_key_does_not_go_here = {}) .flat_map { |associated_record| foreign_key_pair = { foreign_key => associated_record.fetch(key), } [ associated_record, parent_record.merge(foreign_key_pair), ] } } end |
#eager_superset(superset, associated_dataset) ⇒ Object
27 28 29 30 31 |
# File 'lib/sequel_mapper/many_to_one_association.rb', line 27 def eager_superset(superset, associated_dataset) Dataset.new( superset.where(key => associated_dataset.select(foreign_key)).to_a ) end |