Module: MetaMissing

Includes:
Patched
Defined in:
lib/meta_missing.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/meta_missing.rb', line 34

def method_missing(name, *args, &block)
  if handler = method_handlers.find { |handler| handler[:regexp] =~ name.to_s }
    match = name.to_s.match(handler[:regexp])
    
    if block_given?
      class_eval do
        alias_method :orig_block_given?, :block_given?
        def block_given?; true; end
      end
      
      MetaMissing.bind(handler[:block], self)[match, name, *args, &block].tap do
        class_eval do
          alias_method :block_given?, :orig_block_given?
          remove_method :orig_block_given?
        end
      end
    else
      instance_exec(match, name, *args, &handler[:block])
    end
  else super end
end

Class Method Details

.bind(proc, context) ⇒ Object



30
31
32
# File 'lib/meta_missing.rb', line 30

def self.bind(proc, context)
  proc.bind(context)
end

.prepended(base) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/meta_missing.rb', line 21

def self.prepended(base)
  base.class_eval do
    extend ClassMethods

    class_attribute :method_handlers
    self.method_handlers = []
  end
end