Class: SDL::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/sdl/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#modelsArray<Model> (readonly)

All models that have been defined

Returns:



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

Parameters:

  • name (Symbol, String)

    name of the model to find

Returns:



19
20
21
# File 'lib/sdl/schema.rb', line 19

def find_model(name)
  models.find { |m| m.name == name.to_s }
end

#model(name, **options, &block) ⇒ Object

Adds a new Model to the schema

Examples:

model :user do
  attribute :name, :string
end

Parameters:

  • name (Symbol)
  • options (Hash)


31
32
33
# File 'lib/sdl/schema.rb', line 31

def model(name, **options, &block)
  @models << Model.new(name, **options, &block)
end