Class: Stateful::States
- Inherits:
-
Object
- Object
- Stateful::States
- Defined in:
- lib/Stateful/States.rb
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
Instance Method Summary collapse
- #detect(candidate_state) ⇒ Object (also: #find)
- #final_state(*state_names) ⇒ Object (also: #final_state=)
- #final_states(*state_names) ⇒ Object
- #find_or_create(state_name, options = {}) ⇒ Object
- #initial_state(state_name = nil, options = {}, &block) ⇒ Object
-
#initialize(klass, options = {}) ⇒ States
constructor
A new instance of States.
- #missing?(state) ⇒ Boolean (also: #new_state?)
- #present?(state) ⇒ Boolean
- #state(state_name, options = {}, &block) ⇒ Object
- #stateful(options = {}, &block) ⇒ Object
Constructor Details
#initialize(klass, options = {}) ⇒ States
Returns a new instance of States.
11 12 13 14 15 16 17 |
# File 'lib/Stateful/States.rb', line 11 def initialize(klass, = {}) @klass = klass @options = @all = [] @initial_state = nil @final_states = [] end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
9 10 11 |
# File 'lib/Stateful/States.rb', line 9 def all @all end |
Instance Method Details
#detect(candidate_state) ⇒ Object Also known as: find
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/Stateful/States.rb', line 19 def detect(candidate_state) candidate_state_name = ( if candidate_state.is_a?(State) candidate_state.name else candidate_state && candidate_state.to_sym end ) self.all.detect{|state| state.name == candidate_state_name} end |
#final_state(*state_names) ⇒ Object Also known as: final_state=
56 57 58 |
# File 'lib/Stateful/States.rb', line 56 def final_state(*state_names) final_states(*state_names).first end |
#final_states(*state_names) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/Stateful/States.rb', line 61 def final_states(*state_names) state_names = state_names.flatten if !state_names.empty? state_names.each do |state_name| final_state = State.new(state_name) @final_states << final_state all << final_state end @final_states else @final_states end end |
#find_or_create(state_name, options = {}) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/Stateful/States.rb', line 40 def find_or_create(state_name, = {}) find(state_name) || ( state = State.new(state_name, ) all << state state ) end |
#initial_state(state_name = nil, options = {}, &block) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/Stateful/States.rb', line 48 def initial_state(state_name = nil, = {}, &block) if state_name @initial_state = state(state_name, , &block) else @initial_state end end |
#missing?(state) ⇒ Boolean Also known as: new_state?
31 32 33 |
# File 'lib/Stateful/States.rb', line 31 def missing?(state) !detect(state) end |
#present?(state) ⇒ Boolean
36 37 38 |
# File 'lib/Stateful/States.rb', line 36 def present?(state) !!detect(state) end |
#state(state_name, options = {}, &block) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/Stateful/States.rb', line 75 def state(state_name, = {}, &block) .merge!(non_deterministic_event_ordering: global_non_deterministic_event_ordering?) state = find_or_create(state_name, ) state.instance_eval(&block) if block state.transitions.each do |transition| @klass.define_event_method(transition) end @klass.define_status_predicate_method(state_name) state end |
#stateful(options = {}, &block) ⇒ Object
86 87 88 89 |
# File 'lib/Stateful/States.rb', line 86 def stateful( = {}, &block) @options = instance_eval(&block) if block end |