class Dbee::Schema

A schema represents an entire graph of related models.

Attributes

models[R]
models_by_name[R]

Public Class Methods

new(schema_config) click to toggle source
# File lib/dbee/schema.rb, line 16
def initialize(schema_config)
  @models_by_name = Model.make_keyed_by(:name, schema_config)

  freeze
end

Public Instance Methods

==(other) click to toggle source
# File lib/dbee/schema.rb, line 52
def ==(other)
  other.instance_of?(self.class) && other.send(:models_by_name) == models_by_name
end
Also aliased as: eql?
eql?(other)
Alias for: ==
expand_query_path(model, key_path, query_path = []) click to toggle source

Given a Dbee::Model and Dbee::KeyPath, this returns a list of Dbee::Relationship and Dbee::Model tuples that lie on the key path. The returned list is a two dimensional array in the form of [[relationship, model], [relationship2, model2]], etc. The relationships and models correspond to each ancestor part of the key path.

The key_path argument can be either a Dbee::KeyPath or an array of string ancestor names.

An exception is raised of the provided key_path contains relationship names that do not exist in this schema.

# File lib/dbee/schema.rb, line 34
def expand_query_path(model, key_path, query_path = [])
  ancestors = key_path.respond_to?(:ancestor_names) ? key_path.ancestor_names : key_path
  relationship_name = ancestors.first
  return query_path unless relationship_name

  relationship = relationship_for_name!(model, relationship_name)
  join_model = model_for_name!(relationship.model_name)
  expand_query_path(
    join_model,
    ancestors.drop(1),
    query_path + [[relationship_for_name!(model, relationship_name), join_model]]
  )
end
model_for_name!(model_name) click to toggle source
# File lib/dbee/schema.rb, line 48
def model_for_name!(model_name)
  models_by_name[model_name.to_s] || raise(Model::ModelNotFoundError, model_name)
end

Private Instance Methods

relationship_for_name!(model, rel_name) click to toggle source
# File lib/dbee/schema.rb, line 61
def relationship_for_name!(model, rel_name)
  model.relationship_for_name(rel_name) ||
    raise("model '#{model.name}' does not have a '#{rel_name}' relationship")
end