Class: Jsm::States
- Inherits:
-
Object
- Object
- Jsm::States
- Defined in:
- lib/jsm/states.rb
Overview
Jsm::States job is to collect all states
Instance Attribute Summary collapse
-
#list ⇒ Object
readonly
Returns the value of attribute list.
Instance Method Summary collapse
-
#add_state(state_name, params = {}) ⇒ Object
register new state into the list.
- #has_state?(state_name) ⇒ Boolean
- #initial_state ⇒ Object
-
#initialize ⇒ States
constructor
A new instance of States.
Constructor Details
#initialize ⇒ States
Returns a new instance of States.
6 7 8 |
# File 'lib/jsm/states.rb', line 6 def initialize @list = [] end |
Instance Attribute Details
#list ⇒ Object (readonly)
Returns the value of attribute list.
3 4 5 |
# File 'lib/jsm/states.rb', line 3 def list @list end |
Instance Method Details
#add_state(state_name, params = {}) ⇒ Object
register new state into the list
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jsm/states.rb', line 13 def add_state(state_name, params = {}) initial = params.fetch(:initial) { false } if !state_unique?(state_name) raise Jsm::NotUniqueStateError, "state #{state_name} has been defined" end if initial && !initial_state.nil? raise Jsm::InvalidStateError,"can not set initial state to #{state_name}. current initial state is #{initial_state.name}" end state = create_state(state_name, initial) list.push(state) @initial_state = state if state.initial end |
#has_state?(state_name) ⇒ Boolean
33 34 35 |
# File 'lib/jsm/states.rb', line 33 def has_state?(state_name) list.any? { |state| state.name == state_name} end |
#initial_state ⇒ Object
29 30 31 |
# File 'lib/jsm/states.rb', line 29 def initial_state @initial_state end |