Class: FlowMachine::WorkflowState

Inherits:
Object
  • Object
show all
Extended by:
CallbackDsl, ClassMethods
Defined in:
lib/flow_machine/workflow_state.rb

Defined Under Namespace

Modules: CallbackDsl, ClassMethods

Instance Attribute Summary collapse

Attributes included from ClassMethods

#expose_to_workflow_methods, #state_callbacks

Instance Method Summary collapse

Methods included from ClassMethods

event, expose_to_workflow, state_name

Methods included from CallbackDsl

after_change, after_enter, before_change, on_enter, on_exit

Constructor Details

#initialize(workflow) ⇒ WorkflowState

Returns a new instance of WorkflowState.



108
109
110
111
# File 'lib/flow_machine/workflow_state.rb', line 108

def initialize(workflow)
  @workflow = workflow
  @guard_errors = []
end

Instance Attribute Details

#guard_errorsObject

Returns the value of attribute guard_errors.



7
8
9
# File 'lib/flow_machine/workflow_state.rb', line 7

def guard_errors
  @guard_errors
end

#workflowObject (readonly)

Returns the value of attribute workflow.



6
7
8
# File 'lib/flow_machine/workflow_state.rb', line 6

def workflow
  @workflow
end

Instance Method Details

#==(other) ⇒ Object



148
149
150
# File 'lib/flow_machine/workflow_state.rb', line 148

def ==(other)
  self.class == other.class
end

#fire_callback_list(callbacks, changes = {}) ⇒ Object



113
114
115
116
117
# 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 = {}) ⇒ Object



119
120
121
122
123
# 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

#nameObject



144
145
146
# File 'lib/flow_machine/workflow_state.rb', line 144

def name
  self.class.state_name
end

#run_workflow_method(method_name, *args, &block) ⇒ Object

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

Raises:

  • (NoMethodError)


128
129
130
131
132
133
# 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 = {}) ⇒ Object



135
136
137
138
139
140
141
142
# 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