Module: Moody::Context

Defined in:
lib/moody.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



3
4
5
6
7
8
# File 'lib/moody.rb', line 3

def self.extended(base)
  base.class_eval do
    attr_accessor :state
    delegate_to_state :switch_to
  end
end

Instance Method Details

#delegate_to_state(*methods) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moody.rb', line 18

def delegate_to_state(*methods)
  method_definitions = methods.inject("") do |code, method|
    code + "      def \#{method}(*args, &block)\n        state.\#{method}(*args, &block)\n      end\n    RUBY\n  end\n\n  module_eval(method_definitions)\nend\n"

#initial_state(state) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/moody.rb', line 10

def initial_state(state)
  module_eval "    def state\n      @state ||= \#{state}.new(self)\n    end\n  RUBY\nend\n"