Class: Jsm::EventExecutor::ActiveModel

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

Direct Known Subclasses

ActiveRecord

Instance Attribute Summary

Attributes inherited from Base

#validators

Instance Method Summary collapse

Methods inherited from Base

#execute!, #initialize

Constructor Details

This class inherits a constructor from Jsm::EventExecutor::Base

Instance Method Details

#can_be_executed?(event, obj) ⇒ Boolean

check if the obj possible to execute the event

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jsm/event_executor/active_model.rb', line 14

def can_be_executed?(event, obj)
  state = event.can_be_transitioning_to(obj)
  attribute_name = obj.class.state_machine.attribute_name
  obj.valid?
  if state
    validators.validate(state.to, obj)
  else
    obj.errors.add(attribute_name, 'no transitions match')
  end
  obj.errors.empty?
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



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

def execute(event, obj)
  if can_be_executed?(event, obj)
    event.execute(obj)
  else
    false
  end
end