Module: ModelOrchestration::Base::ClassMethods

Defined in:
lib/model_orchestration/base.rb

Instance Method Summary collapse

Instance Method Details

#nested_model(model) ⇒ Object

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


30
31
32
33
34
# 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 = {}) ⇒ Object

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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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