Sha256: 33e8fecc661aa910ad8e12a469c12b7aaf35a2be22332a981a3adc70968461ce

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require "terrestrial/dataset"

module Terrestrial
  class ManyToOneAssociation
    def initialize(mapping_name:, foreign_key:, key:, proxy_factory:)
      @mapping_name = mapping_name
      @foreign_key = foreign_key
      @key = key
      @proxy_factory = proxy_factory
    end

    def mapping_names
      [mapping_name]
    end

    attr_reader :mapping_name

    attr_reader :foreign_key, :key, :proxy_factory
    private     :foreign_key, :key, :proxy_factory

    def build_proxy(data_superset:, loader:, record:)
      proxy_factory.call(
        query: build_query(data_superset, record),
        loader: loader,
        preloaded_data: {
          key => foreign_key_value(record),
        },
      )
    end

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

    def build_query((superset), record)
      superset.where(key => foreign_key_value(record))
    end

    def dump(parent_record, collection, depth, &block)
      collection.flat_map { |object|
        block.call(mapping_name, object, _foreign_key_does_not_go_here = {}, depth + depth_modifier)
      }
    end
    alias_method :delete, :dump

    def extract_foreign_key(record)
      {
        foreign_key => record.fetch(key),
      }
    end

    private

    def foreign_key_value(record)
      record.fetch(foreign_key)
    end

    def depth_modifier
      -1
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terrestrial-0.1.1 lib/terrestrial/many_to_one_association.rb