Module: Listen::FSM
- Included in:
- Event::Loop, Listener
- Defined in:
- lib/listen/fsm.rb
Defined Under Namespace
Modules: ClassMethods Classes: State
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Current state of the FSM, stored as a symbol.
Class Method Summary collapse
-
.included(klass) ⇒ Object
Included hook to extend class methods.
Instance Method Summary collapse
- #current_state ⇒ Object private
-
#initialize_fsm ⇒ Object
NOTE: including classes must call initialize_fsm from their initialize method.
- #transition(new_state_name) ⇒ Object private
-
#transition!(new_state_name) ⇒ Object
private
Low-level, immediate state transition with no checks or callbacks.
- #transition_with_callbacks!(new_state) ⇒ Object private
- #validate_and_sanitize_new_state(new_state_name) ⇒ Object private
-
#wait_for_state(*wait_for_states, timeout: nil) ⇒ Object
checks for one of the given states to wait for if not already, waits for a state change (up to timeout seconds--
nil
means infinite) returns truthy iff the transition to one of the desired state has occurred.
Instance Attribute Details
#state ⇒ Object (readonly)
Current state of the FSM, stored as a symbol
50 51 52 |
# File 'lib/listen/fsm.rb', line 50 def state @state end |
Class Method Details
.included(klass) ⇒ Object
Included hook to extend class methods
10 11 12 |
# File 'lib/listen/fsm.rb', line 10 def self.included(klass) klass.send :extend, ClassMethods end |
Instance Method Details
#current_state ⇒ Object (private)
108 109 110 |
# File 'lib/listen/fsm.rb', line 108 def current_state self.class.states[@state] end |
#initialize_fsm ⇒ Object
NOTE: including classes must call initialize_fsm from their initialize method.
42 43 44 45 46 47 |
# File 'lib/listen/fsm.rb', line 42 def initialize_fsm @fsm_initialized = true @state = self.class.start_state @mutex = ::Mutex.new @state_changed = ::ConditionVariable.new end |
#transition(new_state_name) ⇒ Object (private)
69 70 71 72 73 74 |
# File 'lib/listen/fsm.rb', line 69 def transition(new_state_name) new_state_name.is_a?(Symbol) or raise ArgumentError, "state name must be a Symbol (got #{new_state_name.inspect})" if (new_state = validate_and_sanitize_new_state(new_state_name)) transition_with_callbacks!(new_state) end end |
#transition!(new_state_name) ⇒ Object (private)
Low-level, immediate state transition with no checks or callbacks.
77 78 79 80 81 82 83 84 85 |
# File 'lib/listen/fsm.rb', line 77 def transition!(new_state_name) new_state_name.is_a?(Symbol) or raise ArgumentError, "state name must be a Symbol (got #{new_state_name.inspect})" @fsm_initialized or raise ArgumentError, "FSM not initialized. You must call initialize_fsm from initialize!" @mutex.synchronize do yield if block_given? @state = new_state_name @state_changed.broadcast end end |
#transition_with_callbacks!(new_state) ⇒ Object (private)
103 104 105 106 |
# File 'lib/listen/fsm.rb', line 103 def transition_with_callbacks!(new_state) transition! new_state.name new_state.call(self) end |
#validate_and_sanitize_new_state(new_state_name) ⇒ Object (private)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/listen/fsm.rb', line 87 def validate_and_sanitize_new_state(new_state_name) return nil if @state == new_state_name if current_state && !current_state.valid_transition?(new_state_name) valid = current_state.transitions.map(&:to_s).join(', ') msg = "#{self.class} can't change state from '#{@state}' to '#{new_state_name}', only to: #{valid}" raise ArgumentError, msg end unless (new_state = self.class.states[new_state_name]) new_state_name == self.class.start_state or raise ArgumentError, "invalid state for #{self.class}: #{new_state_name}" end new_state end |
#wait_for_state(*wait_for_states, timeout: nil) ⇒ Object
checks for one of the given states to wait for
if not already, waits for a state change (up to timeout seconds--nil
means infinite)
returns truthy iff the transition to one of the desired state has occurred
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/listen/fsm.rb', line 55 def wait_for_state(*wait_for_states, timeout: nil) wait_for_states.each do |state| state.is_a?(Symbol) or raise ArgumentError, "states must be symbols (got #{state.inspect})" end @mutex.synchronize do if !wait_for_states.include?(@state) @state_changed.wait(@mutex, timeout) end wait_for_states.include?(@state) end end |