Module: ModelOrchestration::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/model_orchestration/base.rb

Overview

The module containing the base functionality. This includes class methods to specify nested models and dependencies, as well as ActiveModel-like functionality, such as attribute accessors for the nested models and validation methods.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *args, &block) ⇒ Object

Implements attribute accessor methods for nested models.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/model_orchestration/base.rb', line 151

def method_missing(message, *args, &block)
  if @nested_model_instances.include?(message)
    # Get nested model accessor
    @nested_model_instances[message]
  elsif message =~ /^([^=]+)=$/
    # Set nested model accessor
    attr = message.to_s.chop.to_sym
    if @nested_model_instances.include?(attr)
      @nested_model_instances[attr] = args.first
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object

Get a nested model by name (symbol).

class Signup
  include ModelOrchestration::Base

  nested_model :company
  nested_model :employee

  nested_model_dependency from: :employee, to: :company
end

  = Signup.new
company = [:company]

Because ActiveRecord classes also support the [] method, it can be chained to get attributes of nested models.

company_name = [:company][:name]


127
128
129
# File 'lib/model_orchestration/base.rb', line 127

def [](key)
  send key
end

#[]=(key, value) ⇒ Object

Set a nested model by name (symbol).

class Signup
  include ModelOrchestration::Base

  nested_model :company
  nested_model :employee

  nested_model_dependency from: :employee, to: :company
end

  = Signup.new
[:company] = Company.new


145
146
147
# File 'lib/model_orchestration/base.rb', line 145

def []=(key, value)
  send "#{key}=", value
end

#initialize(attrs = {}) ⇒ Object

Instantiate the model and all nested models. Attributes can be submitted as a hash and will be handed over to the nested models.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/model_orchestration/base.rb', line 89

def initialize(attrs = {})
  @nested_model_instances = {}

  self.nested_models.each do |model|
    klass = model.to_s.classify.constantize
  
    if attrs.include?(model)
      @nested_model_instances[model] = klass.new(attrs[model])
    else
      @nested_model_instances[model] = klass.new
    end
  end

  self.dependencies.each do |from, to|
    @nested_model_instances[from].public_send("#{to.to_s}=", @nested_model_instances[to])
  end
end

#invalid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/model_orchestration/base.rb', line 185

def invalid?(context = nil)
  !valid?(context)
end

#valid?(context = nil) ⇒ Boolean Also known as: validate

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
# File 'lib/model_orchestration/base.rb', line 170

def valid?(context = nil)
  valid = true

  @nested_model_instances.each do |key, instance|
    valid = false unless instance.valid?(context)
  end
  
  valid
end

#validate!(context = nil) ⇒ Object



191
192
193
# File 'lib/model_orchestration/base.rb', line 191

def validate!(context = nil)
  valid?(context) || raise_validation_error
end