class SequelMapper::Configurations::ConventionalAssociationConfiguration

Constants

DEFAULT

Attributes

datastore[R]
local_mapping[R]
local_mapping_name[R]
mappings[R]

Public Class Methods

new(mapping_name, mappings, datastore) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 13
def initialize(mapping_name, mappings, datastore)
  @local_mapping_name = mapping_name
  @mappings = mappings
  @local_mapping = mappings.fetch(local_mapping_name)
  @datastore = datastore
end

Public Instance Methods

belongs_to(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 54
def belongs_to(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT)
  defaults = {
    key: :id,
    foreign_key: [association_name, "_id"].join.to_sym,
    mapping_name: INFLECTOR.pluralize(association_name).to_sym,
  }

  specified = {
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)

  associated_mapping_name = config.fetch(:mapping_name)
  associated_mapping = mappings.fetch(associated_mapping_name)

  local_mapping.add_association(
    association_name,
    belongs_to_mapper(**config)
  )
end
has_many(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 25
def has_many(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT)
  defaults = {
    mapping_name: association_name,
    foreign_key: [INFLECTOR.singularize(local_mapping_name), "_id"].join.to_sym,
    key: :id,
    order_fields: [],
    order_direction: "ASC",
  }

  specified = {
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
    order_fields: order_fields,
    order_direction: order_direction,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)
  associated_mapping_name = config.fetch(:mapping_name)
  associated_mapping = mappings.fetch(associated_mapping_name)

  local_mapping.add_association(
    association_name,
    has_many_mapper(**config)
  )
end
has_many_through(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, through_mapping_name: DEFAULT, association_key: DEFAULT, association_foreign_key: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 80
def has_many_through(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, through_mapping_name: DEFAULT, association_key: DEFAULT, association_foreign_key: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT)
  defaults = {
    mapping_name: association_name,
    key: :id,
    association_key: :id,
    foreign_key: [INFLECTOR.singularize(local_mapping_name), "_id"].join.to_sym,
    association_foreign_key: [INFLECTOR.singularize(association_name), "_id"].join.to_sym,
    order_fields: [],
    order_direction: "ASC",
  }

  specified = {
    mapping_name: mapping_name,
    key: key,
    association_key: association_key,
    foreign_key: foreign_key,
    association_foreign_key: association_foreign_key,
    order_fields: order_fields,
    order_direction: order_direction,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)
  associated_mapping = mappings.fetch(config.fetch(:mapping_name))

  if through_mapping_name == DEFAULT
    through_mapping_name = [
      associated_mapping.name,
      local_mapping.name,
    ].sort.join("_to_").to_sym
  end

  join_table_name = mappings.fetch(through_mapping_name).namespace
  config = config
    .merge(
      through_mapping_name: through_mapping_name,
      through_dataset: datastore[join_table_name.to_sym],
    )

  local_mapping.add_association(
    association_name,
    has_many_through_mapper(**config)
  )
end

Private Instance Methods

belongs_to_mapper(mapping_name:, key:, foreign_key:) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 138
def belongs_to_mapper(mapping_name:, key:, foreign_key:)
  ManyToOneAssociation.new(
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
    proxy_factory: single_object_proxy_factory,
  )
end
collection_proxy_factory() click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 170
def collection_proxy_factory
  ->(query:, loader:, mapping_name:) {
    CollectionMutabilityProxy.new(
      QueryableLazyDatasetLoader.new(
        query,
        loader,
        mappings.fetch(mapping_name).subsets,
      )
    )
  }
end
has_many_mapper(mapping_name:, key:, foreign_key:, order_fields:, order_direction:) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 128
def has_many_mapper(mapping_name:, key:, foreign_key:, order_fields:, order_direction:)
  OneToManyAssociation.new(
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
    order: query_order(order_fields, order_direction),
    proxy_factory: collection_proxy_factory,
  )
end
has_many_through_mapper(mapping_name:, key:, foreign_key:, association_key:, association_foreign_key:, through_mapping_name:, through_dataset:, order_fields:, order_direction:) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 147
def has_many_through_mapper(mapping_name:, key:, foreign_key:, association_key:, association_foreign_key:, through_mapping_name:, through_dataset:, order_fields:, order_direction:)
  ManyToManyAssociation.new(
    mapping_name: mapping_name,
    key: key,
    foreign_key: foreign_key,
    association_key: association_key,
    association_foreign_key: association_foreign_key,
    join_mapping_name: through_mapping_name,
    join_dataset: through_dataset,
    proxy_factory: collection_proxy_factory,
    order: query_order(order_fields, order_direction),
  )
end
query_order(fields, direction) click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 182
def query_order(fields, direction)
  QueryOrder.new(fields: fields, direction: direction)
end
single_object_proxy_factory() click to toggle source
# File lib/sequel_mapper/configurations/conventional_association_configuration.rb, line 161
def single_object_proxy_factory
  ->(query:, loader:, preloaded_data:) {
    LazyObjectProxy.new(
      ->{ loader.call(query.first) },
      preloaded_data,
    )
  }
end