Class: Moody::State

Inherits:
Object
  • Object
show all
Defined in:
lib/moody.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ State

Returns a new instance of State.



34
35
36
# File 'lib/moody.rb', line 34

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



32
33
34
# File 'lib/moody.rb', line 32

def context
  @context
end

Instance Method Details

#enterObject



47
48
# File 'lib/moody.rb', line 47

def enter
end

#leaveObject



44
45
# File 'lib/moody.rb', line 44

def leave
end

#switch_to(next_state) ⇒ Object



38
39
40
41
42
# File 'lib/moody.rb', line 38

def switch_to(next_state)
  context.state.leave if context.state.respond_to?(:leave)
  context.state = next_state.new(context)
  context.state.enter if context.state.respond_to?(:enter)
end