class SDL::Schema

Attributes

models[R]

All models that have been defined @return [Array<Model>]

Public Class Methods

new(&block) click to toggle source

@api private

# File lib/sdl/schema.rb, line 11
def initialize(&block)
  @models = []
  instance_eval(&block) if block_given?
end

Public Instance Methods

depsort!() click to toggle source

Sort all {Model} instances in order of dependency @raise [CircularDependencyError]

# 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) click to toggle source

Find a model by name @param name [Symbol,String] name of the model to find @return [Model,nil]

# File lib/sdl/schema.rb, line 19
def find_model(name)
  models.find { |m| m.name == name.to_s }
end
model(name, **options, &block) click to toggle source

Adds a new {Model} to the schema @param name [Symbol] @param options [Hash]

@example

model :user do
  attribute :name, :string
end
# File lib/sdl/schema.rb, line 31
def model(name, **options, &block)
  @models << Model.new(name, **options, &block)
end