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
# File 'lib/jsm/event_executor/base.rb', line 5

def initialize(params = {})
  @validators = params[:validators] || Jsm::Validators.new
end

Instance Attribute Details

#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)


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