Class: Terrestrial::OneToManyAssociation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_keysObject



21
22
23
# File 'lib/terrestrial/one_to_many_association.rb', line 21

def local_foreign_keys
  []
end

#mapping_namesObject



13
14
15
# File 'lib/terrestrial/one_to_many_association.rb', line 13

def mapping_names
  [mapping_name]
end

#outgoing_foreign_keysObject



17
18
19
# File 'lib/terrestrial/one_to_many_association.rb', line 17

def outgoing_foreign_keys
  [foreign_key]
end