Class: Dbee::DslSchemaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dbee/dsl_schema_builder.rb

Overview

Builds a Dbee::Schema given a Dbee::Base (DSL) model. Note that this class does not exist in the “Dsl” module as it is not used for defining the DSL itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl_model, key_chain) ⇒ DslSchemaBuilder

Returns a new instance of DslSchemaBuilder.



17
18
19
20
21
22
# File 'lib/dbee/dsl_schema_builder.rb', line 17

def initialize(dsl_model, key_chain)
  @dsl_model = dsl_model || ArgumentError('dsl_model is required')
  @key_chain = key_chain || ArgumentError('key_chain is required')

  freeze
end

Instance Attribute Details

#dsl_modelObject (readonly)

:nodoc:



15
16
17
# File 'lib/dbee/dsl_schema_builder.rb', line 15

def dsl_model
  @dsl_model
end

#key_chainObject (readonly)

:nodoc:



15
16
17
# File 'lib/dbee/dsl_schema_builder.rb', line 15

def key_chain
  @key_chain
end

Instance Method Details

#to_schemaObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dbee/dsl_schema_builder.rb', line 24

def to_schema
  schema_spec = { dsl_model.inflected_class_name => model_config(dsl_model) }

  ancestor_paths(key_chain).each do |key_path|
    start_model = dsl_model

    key_path.ancestor_names.each do |association_name|
      start_model = append_model_and_relationship(schema_spec, start_model, association_name)
    end
  end

  Schema.new(schema_spec)
end