class Jsm::Event

Jsm::Event handle event related task registered by the main module. It do transition process from one state to another state

Attributes

attribute_name[RW]
name[R]
states[R]
transitions[R]

Public Class Methods

new(name, params = {}, &block) click to toggle source
# File lib/jsm/event.rb, line 13
def initialize(name, params = {}, &block)
  @name = name
  @states = params[:states]
  @transitions = []
  instance_eval(&block) if block_given?
end

Public Instance Methods

can_be_executed?(object) click to toggle source

method to check whether this event can be executed

# File lib/jsm/event.rb, line 53
def can_be_executed?(object)
  from_states = transitions.map(&:from).flatten
  from_states.include?(object.current_state)
end
can_be_transitioning_to(object) click to toggle source

check when running this event, which transitions can change the state of object state return the transition object

# File lib/jsm/event.rb, line 48
def can_be_transitioning_to(object)
  transitions.find{ |transition| transition.from.include?(obj_state(object)) }
end
execute(object) click to toggle source

execute the event, and do a transition if the object current state match with the from state of a transition

# File lib/jsm/event.rb, line 36
def execute(object)
  transition = can_be_transitioning_to(object)
  if transition
    change_state_obj(object, transition.to)
    true
  else
    false
  end
end
transition(params = {}) click to toggle source

register a transition into the. When Event is executed, these transitions is transitioning an object into `to` state, if their current state match with one of the `from` state. the argument input is params `from` and params `to`. Both params should be exist

# File lib/jsm/event.rb, line 25
def transition(params = {})
  validate_params(params)
  validate_state_transition(params)
  from = params[:from].is_a?(Array) ? params[:from] : [params[:from]]
  transition = Jsm::Transition.new(from, params[:to])
  @transitions.push(transition)
end

Private Instance Methods

change_state_obj(object, to_state) click to toggle source
# File lib/jsm/event.rb, line 64
def change_state_obj(object, to_state)
  object.send("jsm_set_state", to_state)
end
obj_state(object) click to toggle source
# File lib/jsm/event.rb, line 60
def obj_state(object)
  object.current_state
end
validate_params(params = {}) click to toggle source
# File lib/jsm/event.rb, line 68
def validate_params(params = {})
  from = params[:from]
  if (from.respond_to?(:empty) && from.empty?) || !from
    raise ArgumentError, "transition is invalid, missing required parameter from"
  end

  to = params[:to]

  if (to.respond_to?(:empty) && to.empty?) || !to
    raise ArgumentError, "transition is invalid, missing required parameter to"
  end
end
validate_state_transition(params = {}) click to toggle source
# File lib/jsm/event.rb, line 81
def validate_state_transition(params = {})
  from = Array(params[:from])
  to = params[:to]
  invalid_state = from.select {|state_name| !states.has_state?(state_name) }
  unless invalid_state.empty?
    raise Jsm::InvalidTransitionError, "parameter from is invalid. there is no state #{invalid_state.join(', ')} in list"
  end

  unless states.has_state?(to)
    raise Jsm::InvalidTransitionError, "parameter to is invalid. there is no state #{to} in list"
  end
end