Class: Jsm::Base
Overview
this module used as extension for state machine class The DSL is built to define the state, event, and transition that happen
Class Method Summary collapse
-
.attribute_name(attribute_name = nil) ⇒ Object
define attribute name of state attribute in the client class.
-
.event(name, &block) ⇒ Object
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.
-
.events ⇒ Object
get list of all events.
-
.initial_state ⇒ Object
return initial_state if empty return nil.
-
.pre_before(event_name, &block) ⇒ Object
set a before callback for an event.
- .raw_states ⇒ Object
-
.state(name, params = {}) ⇒ Object
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.
-
.states ⇒ Object
list of all states.
-
.validate(state_name, &block) ⇒ Object
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.
-
.validators ⇒ Object
list all validators that exist.
Instance Method Summary collapse
-
#initialize(klass) ⇒ Base
constructor
A new instance of Base.
Methods included from Callbacks
Constructor Details
#initialize(klass) ⇒ Base
Returns a new instance of Base.
85 86 87 88 |
# File 'lib/jsm/base.rb', line 85 def initialize(klass) @klass = klass Jsm::ClientExtension.decorate(@klass, state_machine: self.class) end |
Class Method Details
.attribute_name(attribute_name = nil) ⇒ Object
define attribute name of state attribute in the client class
6 7 8 9 10 11 12 |
# 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) ⇒ Object
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
42 43 44 45 46 47 48 49 |
# 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 ⇒ Object
get list of all events
52 53 54 |
# File 'lib/jsm/base.rb', line 52 def self.events @events ||= {} end |
.initial_state ⇒ Object
return initial_state if empty return nil
33 34 35 |
# File 'lib/jsm/base.rb', line 33 def self.initial_state raw_states.initial_state end |
.pre_before(event_name, &block) ⇒ Object
set a before callback for an event
75 76 77 78 79 |
# 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 ⇒ Object
81 82 83 |
# File 'lib/jsm/base.rb', line 81 def self.raw_states @states ||= Jsm::States.new end |
.state(name, params = {}) ⇒ Object
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
22 23 24 |
# File 'lib/jsm/base.rb', line 22 def self.state(name, params = {}) raw_states.add_state(name, initial: params[:initial]) end |
.states ⇒ Object
list of all states
27 28 29 |
# File 'lib/jsm/base.rb', line 27 def self.states raw_states.list end |
.validate(state_name, &block) ⇒ Object
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
62 63 64 65 66 67 68 |
# 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 ⇒ Object
list all validators that exist
71 72 73 |
# File 'lib/jsm/base.rb', line 71 def self.validators @validators ||= Jsm::Validators.new end |