Class: Jsm::EventExecutor::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jsm/event_executor/base.rb

Overview

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

Direct Known Subclasses

ActiveModel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
# File 'lib/jsm/event_executor/base.rb', line 5

def initialize(params = {})
  @state_machine = params[:state_machine]
  @validators = @state_machine.validators

end

Instance Attribute Details

#state_machineObject (readonly)

Returns the value of attribute state_machine.



4
5
6
# File 'lib/jsm/event_executor/base.rb', line 4

def state_machine
  @state_machine
end

#validatorsObject (readonly)

Returns the value of attribute validators.



4
5
6
# File 'lib/jsm/event_executor/base.rb', line 4

def validators
  @validators
end

Instance Method Details

#can_be_executed?(event, obj) ⇒ Boolean

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

Returns:

  • (Boolean)


22
23
24
25
# 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) ⇒ Object

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



15
16
17
18
19
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
# 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