class Jsm::EventExecutor::Base

this class is the base for adapter it can be extended for ActiveRecord adapter

Attributes

state_machine[R]
validators[R]

Public Class Methods

new(params = {}) click to toggle source
# File lib/jsm/event_executor/base.rb, line 5
def initialize(params = {})
  @state_machine = params[:state_machine]
  @validators = @state_machine.validators

end

Public Instance Methods

can_be_executed?(event, obj) click to toggle source

check if the obj possible to execute the event(passed the validation and can do transition)

# File lib/jsm/event_executor/base.rb, line 22
def can_be_executed?(event, obj)
  state = event.can_be_transitioning_to(obj)
  !!state && validators.validate(state.to, obj)
end
execute(event, obj) click to toggle source

it execute event for the object. If transition failed or invalid by validation toward the object, then it will return false it also run callbacks of the event

# File lib/jsm/event_executor/base.rb, line 15
def execute(event, obj)
  state_machine.run_callback event.name, obj do |obj|
    execute_action(event, obj)
  end
end
execute!(event, obj) click to toggle source

same with execute, but if its failed raise error it also run callbacks of the event however after callbacks only run when success because if its failed will raise error

# File lib/jsm/event_executor/base.rb, line 30
def execute!(event, obj)
  state_machine.run_callback event.name, obj do |obj|
    unless execute_action(event, obj)
      raise Jsm::IllegalTransitionError, "there is no matching transitions or invalid, Cant do event #{event.name}"
    end
    true
  end
end

Private Instance Methods

execute_action(event, obj) click to toggle source
# File lib/jsm/event_executor/base.rb, line 40
def execute_action(event, obj)
  can_be_executed?(event, obj) ? event.execute(obj) : false
end