Class: Dbee::Schema
- Inherits:
-
Object
- Object
- Dbee::Schema
- Extended by:
- Forwardable
- Defined in:
- lib/dbee/schema.rb
Overview
A schema represents an entire graph of related models.
Instance Attribute Summary collapse
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#expand_query_path(model, key_path, query_path = []) ⇒ Object
Given a Dbee::Model and Dbee::KeyPath, this returns a list of Dbee::Relationship and Dbee::Model tuples that lie on the key path.
-
#initialize(schema_config) ⇒ Schema
constructor
A new instance of Schema.
- #model_for_name!(model_name) ⇒ Object
Constructor Details
#initialize(schema_config) ⇒ Schema
Returns a new instance of Schema.
16 17 18 19 20 |
# File 'lib/dbee/schema.rb', line 16 def initialize(schema_config) @models_by_name = Model.make_keyed_by(:name, schema_config) freeze end |
Instance Attribute Details
#models ⇒ Object (readonly)
Returns the value of attribute models.
13 14 15 |
# File 'lib/dbee/schema.rb', line 13 def models @models end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
52 53 54 |
# File 'lib/dbee/schema.rb', line 52 def ==(other) other.instance_of?(self.class) && other.send(:models_by_name) == models_by_name end |
#expand_query_path(model, key_path, query_path = []) ⇒ Object
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.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dbee/schema.rb', line 34 def (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) ( join_model, ancestors.drop(1), query_path + [[relationship_for_name!(model, relationship_name), join_model]] ) end |
#model_for_name!(model_name) ⇒ Object
48 49 50 |
# 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 |