class FlowMachine::WorkflowState

Attributes

guard_errors[RW]
workflow[R]

Public Class Methods

new(workflow) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 108
def initialize(workflow)
  @workflow = workflow
  @guard_errors = []
end

Public Instance Methods

==(other) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 148
def ==(other)
  self.class == other.class
end
fire_callback_list(callbacks, changes = {}) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 113
def fire_callback_list(callbacks, changes = {})
  callbacks.each do |callback|
    callback.call(self, changes)
  end
end
fire_callbacks(event, changes = {}) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 119
def fire_callbacks(event, changes = {})
  return unless self.class.state_callbacks.try(:[], event)

  fire_callback_list self.class.state_callbacks[event], changes
end
name() click to toggle source
# File lib/flow_machine/workflow_state.rb, line 144
def name
  self.class.state_name
end
run_workflow_method(method_name, *args, &block) click to toggle source

Allows method calls to fallback up the object chain so guards and other methods can be defined on the object or workflow as well as the state

# File lib/flow_machine/workflow_state.rb, line 128
def run_workflow_method(method_name, *args, &block)
  target = object_chain(method_name)
  raise NoMethodError.new("undefined method #{method_name}", method_name) unless target

  target.send(method_name, *args, &block)
end
transition(options = {}) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 135
def transition(options = {})
  workflow.transition(options).tap do |new_state|
    if new_state != workflow.previous_state
      workflow.previous_state.fire_callbacks(:on_exit)
      new_state.fire_callbacks(:on_enter)
    end
  end
end

Private Instance Methods

object_chain(method_name) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 168
def object_chain(method_name)
  [self, workflow, object].find { |o| o.respond_to?(method_name, true) }
end
run_guard_methods(guard_methods) click to toggle source
# File lib/flow_machine/workflow_state.rb, line 154
def run_guard_methods(guard_methods)
  self.guard_errors = []
  # Use inject to ensure that all guard methods are run.
  # all? short circuits on first false value
  guard_methods.inject(true) do |valid, guard_method|
    if run_workflow_method(guard_method)
      valid
    else
      guard_errors << guard_method
      false
    end
  end
end