Class: SequelMapper::OneToManyAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_mapper/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/sequel_mapper/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.



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

def mapping_name
  @mapping_name
end

Instance Method Details

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



18
19
20
21
22
23
24
# File 'lib/sequel_mapper/one_to_many_association.rb', line 18

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



45
46
47
48
49
# File 'lib/sequel_mapper/one_to_many_association.rb', line 45

def build_query(superset, record)
  order.apply(
    superset.where(foreign_key => record.fetch(key))
  )
end

#dump(parent_record, collection, &block) ⇒ Object Also known as: delete



26
27
28
29
30
31
32
33
34
# File 'lib/sequel_mapper/one_to_many_association.rb', line 26

def dump(parent_record, collection, &block)
  foreign_key_pair = {
    foreign_key => parent_record.fetch(key),
  }

  collection.flat_map { |associated_object|
    block.call(mapping_name, associated_object, foreign_key_pair)
  }
end

#eager_superset(superset, associated_dataset) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/sequel_mapper/one_to_many_association.rb', line 37

def eager_superset(superset, associated_dataset)
  Dataset.new(
    superset.where(
      foreign_key => associated_dataset.select(key)
    ).to_a
  )
end