Class: StateDesignPattern::StateMachine

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Observable
Defined in:
lib/state_design_pattern/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStateMachine

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

#contextObject (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_stateObject



25
26
27
# File 'lib/state_design_pattern/state_machine.rb', line 25

def current_state
  @state.class
end

#initial_contextObject



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, message = {})
  event = { name: name, source: self }.merge(message)

  changed
  notify_observers(event)

  self
end

#start_stateObject

Raises:

  • (NotImplementedError)


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, message = {})
  transition_to_state(state_class)
  send_event(name, message)
end