Class: Terrestrial::ManyToManyAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/terrestrial/many_to_many_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping_name:, join_mapping_name:, join_dataset:, foreign_key:, key:, proxy_factory:, association_foreign_key:, association_key:, order:) ⇒ ManyToManyAssociation

Returns a new instance of ManyToManyAssociation.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/terrestrial/many_to_many_association.rb', line 6

def initialize(mapping_name:, join_mapping_name:, join_dataset:, foreign_key:, key:, proxy_factory:, association_foreign_key:, association_key:, order:)
  @mapping_name = mapping_name
  @join_mapping_name = join_mapping_name
  @join_dataset = join_dataset
  @foreign_key = foreign_key
  @key = key
  @proxy_factory = proxy_factory
  @association_foreign_key = association_foreign_key
  @association_key = association_key
  @order = order
end

Instance Attribute Details

#join_mapping_nameObject (readonly)

Returns the value of attribute join_mapping_name.



30
31
32
# File 'lib/terrestrial/many_to_many_association.rb', line 30

def join_mapping_name
  @join_mapping_name
end

#mapping_nameObject (readonly)

Returns the value of attribute mapping_name.



30
31
32
# File 'lib/terrestrial/many_to_many_association.rb', line 30

def mapping_name
  @mapping_name
end

Instance Method Details

#build_proxy(data_superset:, loader:, record:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/terrestrial/many_to_many_association.rb', line 35

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, join_superset, parent_record) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/terrestrial/many_to_many_association.rb', line 65

def build_query((superset, join_superset), parent_record)
  subselect_ids = join_superset
    .where(foreign_key => foreign_key_value(parent_record))
    .select(association_foreign_key)

  order
    .apply(superset.where(key => subselect_ids))
    .lazy
    .map { |record| [record, [foreign_keys(parent_record, record)]] }
end

#delete(parent_record, collection, depth, &block) ⇒ Object



84
85
86
# File 'lib/terrestrial/many_to_many_association.rb', line 84

def delete(parent_record, collection, depth, &block)
  flat_list_of_just_join_records(parent_record, collection, depth, &block)
end

#dump(parent_record, collection, depth, &block) ⇒ Object



76
77
78
# File 'lib/terrestrial/many_to_many_association.rb', line 76

def dump(parent_record, collection, depth, &block)
  flat_list_of_records_and_join_records(parent_record, collection, depth, &block)
end

#eager_superset(superset, join_superset, associated_dataset) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/terrestrial/many_to_many_association.rb', line 48

def eager_superset((superset, join_superset), (associated_dataset))
  subselect_data = Dataset.new(
    join_superset
      .where(foreign_key => associated_dataset.select(association_key))
      .to_a
  )

  eager_superset = Dataset.new(
    superset.where(key => subselect_data.select(association_foreign_key)).to_a
  )

  [
    eager_superset,
    subselect_data,
  ]
end

#extract_foreign_key(_record) ⇒ Object



80
81
82
# File 'lib/terrestrial/many_to_many_association.rb', line 80

def extract_foreign_key(_record)
  {}
end

#local_foreign_keysObject



26
27
28
# File 'lib/terrestrial/many_to_many_association.rb', line 26

def local_foreign_keys
  []
end

#mapping_namesObject



18
19
20
# File 'lib/terrestrial/many_to_many_association.rb', line 18

def mapping_names
  [mapping_name, join_mapping_name]
end

#outgoing_foreign_keysObject



22
23
24
# File 'lib/terrestrial/many_to_many_association.rb', line 22

def outgoing_foreign_keys
  []
end