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.



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

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Instance Method Details

#enterObject



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

def enter
end

#leaveObject



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

def leave
end

#switch_to(next_state) ⇒ Object



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

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