Class: SequelMapper::ManyToManyAssociation
- Inherits:
-
Object
- Object
- SequelMapper::ManyToManyAssociation
- Defined in:
- lib/sequel_mapper/many_to_many_association.rb
Defined Under Namespace
Classes: JoinedDataset
Instance Attribute Summary collapse
-
#join_mapping_name ⇒ Object
readonly
Returns the value of attribute join_mapping_name.
-
#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, parent_record) ⇒ Object
- #delete(parent_record, collection, &block) ⇒ Object
- #dump(parent_record, collection, &block) ⇒ Object
- #eager_superset(superset, associated_dataset) ⇒ Object
-
#initialize(mapping_name:, foreign_key:, key:, proxy_factory:, association_foreign_key:, association_key:, join_mapping_name:, join_dataset:, order:) ⇒ ManyToManyAssociation
constructor
A new instance of ManyToManyAssociation.
Constructor Details
#initialize(mapping_name:, foreign_key:, key:, proxy_factory:, association_foreign_key:, association_key:, join_mapping_name:, join_dataset:, order:) ⇒ ManyToManyAssociation
Returns a new instance of ManyToManyAssociation.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 5 def initialize(mapping_name:, foreign_key:, key:, proxy_factory:, association_foreign_key:, association_key:, join_mapping_name:, join_dataset:, order:) @mapping_name = mapping_name @foreign_key = foreign_key @key = key @proxy_factory = proxy_factory @association_foreign_key = association_foreign_key @association_key = association_key @join_mapping_name = join_mapping_name @join_dataset = join_dataset @order = order end |
Instance Attribute Details
#join_mapping_name ⇒ Object (readonly)
Returns the value of attribute join_mapping_name.
17 18 19 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 17 def join_mapping_name @join_mapping_name end |
#mapping_name ⇒ Object (readonly)
Returns the value of attribute mapping_name.
17 18 19 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 17 def mapping_name @mapping_name end |
Instance Method Details
#build_proxy(data_superset:, loader:, record:) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 22 def build_proxy(data_superset:, loader:, record:) proxy_factory.call( query: build_query(data_superset, record), loader: ->(record_list) { record = record_list.first join_records = record_list.last loader.call(record, join_records) }, mapping_name: mapping_name, ) end |
#build_query(superset, parent_record) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 50 def build_query(superset, parent_record) order .apply( superset.join(join_mapping_name, association_foreign_key => key) .where(foreign_key => foreign_key_value(parent_record)) ) .lazy.map { |record| [record, [foreign_keys(parent_record, record)]] } end |
#delete(parent_record, collection, &block) ⇒ Object
141 142 143 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 141 def delete(parent_record, collection, &block) flat_list_of_just_join_records(parent_record, collection, &block) end |
#dump(parent_record, collection, &block) ⇒ Object
137 138 139 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 137 def dump(parent_record, collection, &block) flat_list_of_records_and_join_records(parent_record, collection, &block) end |
#eager_superset(superset, associated_dataset) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sequel_mapper/many_to_many_association.rb', line 35 def eager_superset(superset, associated_dataset) # TODO: All these keys can be confusing, write some focused tests. eager_join_dataset = Dataset.new( join_dataset .where(foreign_key => associated_dataset.select(association_key)) .to_a ) eager_dataset = superset .where(key => eager_join_dataset.select(association_foreign_key)) .to_a JoinedDataset.new(eager_dataset, eager_join_dataset) end |