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
# File 'lib/moody.rb', line 3

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

Instance Method Details

#delegate_to_state(*methods) ⇒ Object



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

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

  module_eval(method_definitions)
end

#initial_state(state) ⇒ Object



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

def initial_state(state)
  module_eval <<-RUBY
    def state
      @state ||= #{state}.new(self)
    end
  RUBY
end