Class: StateDesignPattern::StateMachine
- Inherits:
-
Object
- Object
- StateDesignPattern::StateMachine
- Extended by:
- Forwardable
- Includes:
- Observable
- Defined in:
- lib/state_design_pattern/state_machine.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #current_state ⇒ Object
- #initial_context ⇒ Object
-
#initialize ⇒ StateMachine
constructor
A new instance of StateMachine.
- #send_event(name, message = {}) ⇒ Object
- #start_state ⇒ Object
- #transition_to_state(state_class) ⇒ Object
- #transition_to_state_and_send_event(state_class, name, message = {}) ⇒ Object
Constructor Details
#initialize ⇒ StateMachine
Returns a new instance of StateMachine.
12 13 14 15 16 |
# File 'lib/state_design_pattern/state_machine.rb', line 12 def initialize initialize_context transition_to_start_state setup_delegation end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/state_design_pattern/state_machine.rb', line 10 def context @context end |
Instance Method Details
#current_state ⇒ Object
25 26 27 |
# File 'lib/state_design_pattern/state_machine.rb', line 25 def current_state @state.class end |
#initial_context ⇒ Object
18 19 |
# File 'lib/state_design_pattern/state_machine.rb', line 18 def initial_context end |
#send_event(name, message = {}) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/state_design_pattern/state_machine.rb', line 38 def send_event(name, = {}) event = { name: name, source: self }.merge() changed notify_observers(event) self end |
#start_state ⇒ Object
21 22 23 |
# File 'lib/state_design_pattern/state_machine.rb', line 21 def start_state raise NotImplementedError end |
#transition_to_state(state_class) ⇒ Object
34 35 36 |
# File 'lib/state_design_pattern/state_machine.rb', line 34 def transition_to_state(state_class) @state = state_class.new(self) end |
#transition_to_state_and_send_event(state_class, name, message = {}) ⇒ Object
29 30 31 32 |
# File 'lib/state_design_pattern/state_machine.rb', line 29 def transition_to_state_and_send_event(state_class, name, = {}) transition_to_state(state_class) send_event(name, ) end |