Class: Terrestrial::OneToManyAssociation
- Inherits:
-
Object
- Object
- Terrestrial::OneToManyAssociation
- Defined in:
- lib/terrestrial/one_to_many_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, depth, &block) ⇒ Object (also: #delete)
- #eager_superset(superset, associated_dataset) ⇒ Object
- #extract_foreign_key(_record) ⇒ Object
-
#initialize(mapping_name:, foreign_key:, key:, order:, proxy_factory:) ⇒ OneToManyAssociation
constructor
A new instance of OneToManyAssociation.
- #local_foreign_keys ⇒ Object
- #mapping_names ⇒ Object
- #outgoing_foreign_keys ⇒ Object
Constructor Details
#initialize(mapping_name:, foreign_key:, key:, order:, proxy_factory:) ⇒ OneToManyAssociation
Returns a new instance of OneToManyAssociation.
5 6 7 8 9 10 11 |
# File 'lib/terrestrial/one_to_many_association.rb', line 5 def initialize(mapping_name:, foreign_key:, key:, order:, proxy_factory:) @mapping_name = mapping_name @foreign_key = foreign_key @key = key @order = order @proxy_factory = proxy_factory end |
Instance Attribute Details
#mapping_name ⇒ Object (readonly)
Returns the value of attribute mapping_name.
25 26 27 |
# File 'lib/terrestrial/one_to_many_association.rb', line 25 def mapping_name @mapping_name end |
Instance Method Details
#build_proxy(data_superset:, loader:, record:) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/terrestrial/one_to_many_association.rb', line 30 def build_proxy(data_superset:, loader:, record:) proxy_factory.call( query: build_query(data_superset, record), loader: loader, mapping_name: mapping_name, ) end |
#build_query(superset, record) ⇒ Object
63 64 65 66 67 |
# File 'lib/terrestrial/one_to_many_association.rb', line 63 def build_query((superset), record) order.apply( superset.where(foreign_key => record.fetch(key)) ) end |
#dump(parent_record, collection, depth, &block) ⇒ Object Also known as: delete
38 39 40 41 42 43 44 45 46 |
# File 'lib/terrestrial/one_to_many_association.rb', line 38 def dump(parent_record, collection, depth, &block) foreign_key_pair = { foreign_key => parent_record.fetch(key), } collection.flat_map { |associated_object| block.call(mapping_name, associated_object, foreign_key_pair, depth + depth_modifier) } end |
#eager_superset(superset, associated_dataset) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/terrestrial/one_to_many_association.rb', line 53 def eager_superset((superset), (associated_dataset)) [ Dataset.new( superset.where( foreign_key => associated_dataset.select(key) ).to_a ) ] end |
#extract_foreign_key(_record) ⇒ Object
49 50 51 |
# File 'lib/terrestrial/one_to_many_association.rb', line 49 def extract_foreign_key(_record) {} end |
#local_foreign_keys ⇒ Object
21 22 23 |
# File 'lib/terrestrial/one_to_many_association.rb', line 21 def local_foreign_keys [] end |
#mapping_names ⇒ Object
13 14 15 |
# File 'lib/terrestrial/one_to_many_association.rb', line 13 def mapping_names [mapping_name] end |
#outgoing_foreign_keys ⇒ Object
17 18 19 |
# File 'lib/terrestrial/one_to_many_association.rb', line 17 def outgoing_foreign_keys [foreign_key] end |