Class: Jsm::Base
- Inherits:
-
Object
- Object
- Jsm::Base
- Defined in:
- lib/jsm/base.rb
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.
-
.state(name, params = {}) ⇒ Object
add new state to class example state :x state :y.
-
.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.
Constructor Details
#initialize(klass) ⇒ Base
Returns a new instance of Base.
63 64 65 66 |
# File 'lib/jsm/base.rb', line 63 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
33 34 35 36 37 38 39 40 |
# File 'lib/jsm/base.rb', line 33 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
43 44 45 |
# File 'lib/jsm/base.rb', line 43 def self.events @events end |
.state(name, params = {}) ⇒ Object
add new state to class example state :x state :y
18 19 20 21 |
# File 'lib/jsm/base.rb', line 18 def self.state(name, params = {}) @states ||= Jsm::States.new @states.add_state(name, initial: params[:initial]) end |
.states ⇒ Object
list of all states
24 25 26 |
# File 'lib/jsm/base.rb', line 24 def self.states @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
53 54 55 56 |
# File 'lib/jsm/base.rb', line 53 def self.validate(state_name, &block) @validators ||= Jsm::Validators.new @validators.add_validator(state_name, Jsm::Validator.new(:state, state_name, &block)) end |
.validators ⇒ Object
list all validators that exist
59 60 61 |
# File 'lib/jsm/base.rb', line 59 def self.validators @validators end |