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
-
#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.
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
5 6 7 |
# File 'lib/jsm/event_executor/base.rb', line 5 def initialize(params = {}) @validators = params[:validators] || Jsm::Validators.new end |
Instance Attribute Details
#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)
17 18 19 20 |
# File 'lib/jsm/event_executor/base.rb', line 17 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
12 13 14 |
# File 'lib/jsm/event_executor/base.rb', line 12 def execute(event, obj) can_be_executed?(event, obj) ? event.execute(obj) : false end |
#execute!(event, obj) ⇒ Object
same with execute, but if its failed raise error
23 24 25 26 27 28 |
# File 'lib/jsm/event_executor/base.rb', line 23 def execute!(event, obj) unless execute(event, obj) raise Jsm::IllegalTransitionError, "there is no matching transitions or invalid, Cant do event #{event.name}" end true end |