module ModelOrchestration::Base::ClassMethods

Public Instance Methods

nested_model(model) click to toggle source

Class method to declare a nested model. This invokes instantiation of the model when the holding model is instantiated. The nested model can be declared by symbol, type or string.

class HoldingClass
  include ModelOrchestration::Base

  nested_model :user
end
# File lib/model_orchestration/base.rb, line 30
def nested_model(model)
  model_key = symbolize(model)

  self.nested_models << model_key
end
nested_model_dependency(args = {}) click to toggle source

Class method to declare a dependency from one nested model to another.

class Signup
  include ModelOrchestration::Base

  nested_model :company
  nested_model :employee

  nested_model_dependency from: :employee, to: :company
end

The example obove might be used to orchestrate the signup process of a user who creates an account representative for his company. In the database schema, company and employee have a 1:n relation, which is represented by nested_model_dependency.

# File lib/model_orchestration/base.rb, line 52
def nested_model_dependency(args = {})
  unless args.include?(:from) && args.include?(:to)
    raise ArgumentError, ":from and :to hash keys must be included."
  end
  
  from = symbolize(args[:from])
  to   = symbolize(args[:to])
  
  if dependency_introduces_cycle?(from, to)
    raise ModelOrchestration::DependencyCycleError, "#{from} is already a dependency of #{to}"
  end
    
  self.dependencies[from] = to
end

Private Instance Methods

dependency_introduces_cycle?(from, to) click to toggle source
# File lib/model_orchestration/base.rb, line 81
def dependency_introduces_cycle?(from, to)
  self.dependencies.include?(to) && (self.dependencies[to] == from)
end
symbolize(arg) click to toggle source
# File lib/model_orchestration/base.rb, line 69
def symbolize(arg)
  if arg.is_a? Symbol
    arg
  elsif arg.is_a? String
    arg.underscore.to_sym
  elsif arg.is_a? Class
    arg.name.underscore.to_sym
  else
    arg.to_s.underscore.to_sym
  end
end