class Terrestrial::ManyToOneAssociation

Attributes

foreign_key[R]
key[R]
mapping_name[R]
proxy_factory[R]

Public Class Methods

new(mapping_name:, foreign_key:, key:, proxy_factory:) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 5
def initialize(mapping_name:, foreign_key:, key:, proxy_factory:)
  @mapping_name = mapping_name
  @foreign_key = foreign_key
  @key = key
  @proxy_factory = proxy_factory
end

Public Instance Methods

build_proxy(data_superset:, loader:, record:) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 29
def build_proxy(data_superset:, loader:, record:)
  foreign_key_nil?(record) ? nil : proxy_factory.call(
      query: build_query(data_superset, record),
      loader: loader,
      preloaded_data: {
        key => foreign_key_value(record),
      },
    )
end
build_query((superset), record) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 47
def build_query((superset), record)
  superset.where(key => foreign_key_value(record))
end
delete(parent_record, collection, depth, &block)
Alias for: dump
dump(parent_record, collection, depth, &block) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 51
def dump(parent_record, collection, depth, &block)
  collection
    .reject(&:nil?)
    .flat_map { |object|
      block.call(mapping_name, object, _foreign_key_does_not_go_here = {}, depth + depth_modifier)
    }
end
Also aliased as: delete
eager_superset((superset), (associated_dataset)) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 39
def eager_superset((superset), (associated_dataset))
  [
    Dataset.new(
      superset.where(key => associated_dataset.select(foreign_key)).to_a
    )
  ]
end
extract_foreign_key(record) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 60
def extract_foreign_key(record)
  {
    foreign_key => record.fetch(key),
  }.reject { |_k, v| v.nil? }
end
local_foreign_keys() click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 20
def local_foreign_keys
  [foreign_key]
end
mapping_names() click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 12
def mapping_names
  [mapping_name]
end
outgoing_foreign_keys() click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 16
def outgoing_foreign_keys
  []
end

Private Instance Methods

depth_modifier() click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 76
def depth_modifier
  -1
end
foreign_key_nil?(record) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 68
def foreign_key_nil?(record)
  foreign_key_value(record).nil?
end
foreign_key_value(record) click to toggle source
# File lib/terrestrial/many_to_one_association.rb, line 72
def foreign_key_value(record)
  record.fetch(foreign_key)
end