class Jsm::Base

this module used as extension for state machine class The DSL is built to define the state, event, and transition that happen

Public Class Methods

attribute_name(attribute_name = nil) click to toggle source

define attribute name of state attribute in the client class

# File lib/jsm/base.rb, line 6
def self.attribute_name(attribute_name = nil)
  if attribute_name.nil?
    @attribute_name
  else
    @attribute_name = attribute_name
  end
end
event(name, &block) click to toggle source

add new event to the class and add its transition example: event :do_this do transition from: :x, to: :y transition from: [:j, :g], to: :z

# File lib/jsm/base.rb, line 42
def self.event(name, &block)
  @events ||= {}
  if !@events[name].nil?
    raise Jsm::InvalidEventError, "event #{name} has been registered"
  end

  @events[name] = Jsm::Event.new(name, states: @states, &block)
end
events() click to toggle source

get list of all events

# File lib/jsm/base.rb, line 52
def self.events
  @events ||= {}
end
initial_state() click to toggle source

return initial_state if empty return nil

# File lib/jsm/base.rb, line 33
def self.initial_state
  raw_states.initial_state
end
new(klass) click to toggle source
# File lib/jsm/base.rb, line 85
def initialize(klass)
  @klass = klass
  Jsm::ClientExtension.decorate(@klass, state_machine: self.class)
end
pre_before(event_name, &block) click to toggle source

set a before callback for an event

# File lib/jsm/base.rb, line 75
def self.pre_before(event_name, &block)
  if !events[event_name]
    raise Jsm::InvalidEventError, "event #{event_name} has not been registered"
  end
end
raw_states() click to toggle source
# File lib/jsm/base.rb, line 81
def self.raw_states
  @states ||= Jsm::States.new
end
state(name, params = {}) click to toggle source

add new state to class example state :x state :y if put params initial true it will be treated as initial_state example: state :x, initial: true

# File lib/jsm/base.rb, line 22
def self.state(name, params = {})
  raw_states.add_state(name, initial: params[:initial])
end
states() click to toggle source

list of all states

# File lib/jsm/base.rb, line 27
def self.states
  raw_states.list
end
validate(state_name, &block) click to toggle source

add validation of a state(when changes to the targeted state, check whether passed this validation or not) example: state :y validate :y do |obj|

obj.name == 'testMe'

end

# File lib/jsm/base.rb, line 62
def self.validate(state_name, &block)
  unless @states.has_state?(state_name)
    raise Jsm::InvalidStateError, "there is no state #{state_name}"
  end

  validators.add_validator(state_name, Jsm::Validator.new(:state, state_name, &block))
end
validators() click to toggle source

list all validators that exist

# File lib/jsm/base.rb, line 71
def self.validators
  @validators ||= Jsm::Validators.new
end