Class: Jsm::EventExecutor::Base
- Inherits:
-
Object
- Object
- Jsm::EventExecutor::Base
- 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
Instance Attribute Summary collapse
-
#state_machine ⇒ Object
readonly
Returns the value of attribute state_machine.
-
#validators ⇒ Object
readonly
Returns the value of attribute validators.
Instance Method Summary collapse
-
#can_be_executed?(event, obj) ⇒ Boolean
check if the obj possible to execute the event(passed the validation and can do transition).
-
#execute(event, obj) ⇒ Object
it execute event for the object.
-
#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.
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
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_machine ⇒ Object (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 |
#validators ⇒ Object (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)
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 |