Class: SDL::Schema
- Inherits:
-
Object
- Object
- SDL::Schema
- Defined in:
- lib/sdl/schema.rb
Instance Attribute Summary collapse
-
#models ⇒ Array<Model>
readonly
All models that have been defined.
Instance Method Summary collapse
-
#depsort! ⇒ Object
Sort all Model instances in order of dependency.
-
#find_model(name) ⇒ Model?
Find a model by name.
-
#initialize(&block) ⇒ Schema
constructor
private
A new instance of Schema.
-
#model(name, **options, &block) ⇒ Object
Adds a new Model to the schema.
Constructor Details
#initialize(&block) ⇒ Schema
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Schema.
11 12 13 14 |
# File 'lib/sdl/schema.rb', line 11 def initialize(&block) @models = [] instance_eval(&block) if block_given? end |
Instance Attribute Details
#models ⇒ Array<Model> (readonly)
All models that have been defined
8 9 10 |
# File 'lib/sdl/schema.rb', line 8 def models @models end |
Instance Method Details
#depsort! ⇒ Object
Sort all Model instances in order of dependency
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sdl/schema.rb', line 37 def depsort! each_node = models.method(:each) each_child = lambda do |model, &block| belongs_to = model.belongs_to_fields.map(&:model_name) children = models.select { |m| belongs_to.include?(m.name) } children.each(&block) end @models = TSort.tsort(each_node, each_child) @models rescue TSort::Cyclic raise CircularDependencyError, "The schema contains a circular dependency." end |
#find_model(name) ⇒ Model?
Find a model by name
19 20 21 |
# File 'lib/sdl/schema.rb', line 19 def find_model(name) models.find { |m| m.name == name.to_s } end |