module FlowMachine::Workflow::ClassMethods

Attributes

callbacks[RW]

Public Instance Methods

after_save(*args, &block) click to toggle source
# File lib/flow_machine/workflow.rb, line 57
def after_save(*args, &block)
  add_callback(:after_save, FlowMachine::Callback.new(*args, &block))
end
after_transition(*args, &block) click to toggle source
# File lib/flow_machine/workflow.rb, line 61
def after_transition(*args, &block)
  add_callback(:after_transition, FlowMachine::Callback.new(*args, &block))
end
before_save(*args, &block) click to toggle source
# File lib/flow_machine/workflow.rb, line 53
def before_save(*args, &block)
  add_callback(:before_save, FlowMachine::Callback.new(*args, &block))
end
refresh_state_methods!() click to toggle source

Mainly to be used in testing, call this method to ensure that any dynamically created state methods get exposed to the workflow

# File lib/flow_machine/workflow.rb, line 29
def refresh_state_methods!
  states.values.each do |state_class|
    add_state_methods_from(state_class)
  end
end
state(state_class) click to toggle source
# File lib/flow_machine/workflow.rb, line 35
def state(state_class)
  name = get_state_name(state_class)
  states[name] = state_class
  add_state_methods_from(state_class)

  define_method "#{name}?" do
    current_state_name.to_s == name.to_s
  end
end
state_attribute(method) click to toggle source
# File lib/flow_machine/workflow.rb, line 45
def state_attribute(method)
  @state_attribute = method
end
state_method() click to toggle source
# File lib/flow_machine/workflow.rb, line 49
def state_method
  @state_attribute || :state
end
state_names() click to toggle source
# File lib/flow_machine/workflow.rb, line 19
def state_names
  states.keys.map(&:to_s)
end
states() click to toggle source
# File lib/flow_machine/workflow.rb, line 23
def states
  @states ||= {}
end

Private Instance Methods

add_callback(hook, callback) click to toggle source
# File lib/flow_machine/workflow.rb, line 67
def add_callback(hook, callback)
  self.callbacks ||= {}
  callbacks[hook] ||= []
  callbacks[hook] << callback
end
add_state_methods_from(state_class) click to toggle source
# File lib/flow_machine/workflow.rb, line 87
def add_state_methods_from(state_class)
  state_class.expose_to_workflow_methods.try(:each) do |method_name|
    define_state_method(method_name) unless method_defined?(method_name)
  end
end
define_state_method(method_name) click to toggle source

Defines an instance method on Workflow that delegates to the current state. If the current state does not support the method, then add an :invalid_event error to the guard_errors.

# File lib/flow_machine/workflow.rb, line 76
def define_state_method(method_name)
  define_method method_name do |*args|
    if current_state.respond_to?(method_name)
      current_state.send(method_name, *args)
    else
      self.guard_errors = [:invalid_event]
      false
    end
  end
end
get_state_name(state_class) click to toggle source
# File lib/flow_machine/workflow.rb, line 93
def get_state_name(state_class)
  state_class.state_name
end