Module: MatchDef

Defined in:
lib/matches.rb

Overview

Defines all the necessary components to allow defining dynamic methods.

Instance Method Summary collapse

Instance Method Details

#matches(regexp, &block) ⇒ Object

Defines a new class method that allows you to define dynamic instance methods. Takes a regular expression and a block, which are stored and called later.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/matches.rb', line 7

def matches(regexp, &block)
  # This evil voodoo scares the crud out of me.
  # It sneaks into the instance upon which we are called
  # and sets a class variable, so we can keep track of what
  # match methods have been defined.
  unless self.send(:class_variable_defined?, :@@match_methods)
    self.send(:class_variable_set, :@@match_methods, [])
    self.send(:class_variable_set, :@@cached_match_methods, [])
  end
  
  append_to_methods = Proc.new do |method|
    current = self.send(:class_variable_get, :@@match_methods)
    self.send(:class_variable_set, :@@match_methods, current + [method])
  end
  
  @@available_methods = Proc.new do
    self.send(:class_variable_get, :@@match_methods)
  end
  
  @@append_to_cached_methods = Proc.new do |method|
    current = self.send(:class_variable_get, :@@cached_match_methods)
    self.send(:class_variable_set, :@@cached_match_methods, current + [method])
  end
  
  # The above statement tweaks the scope, so when we refer to
  # @@match_methods here, what we're really doing is referring to
  # the above @@match_methods.
  self.class_eval do
    append_to_methods.call(MatchMethod.new( :matcher => regexp,
                                            :proc => block ))
  end
  
  self.class_eval {
    
    unless method_defined? :method_missing
      def method_missing(meth, *args, &block); super; end
    end
    
    unless method_defined?(:match_method_missing)
      # Defines a +method_missing+ that is aware of the
      # dynamically-defined methods and will call them if appropriate.
      def match_method_missing(message, *args, &block)
        result = nil
        
        # Attempt to evaluate this using a MatchMethod
        matched = @@available_methods.call.find do |mm|
          if mm.matches?(message)
            
            # Cache the method onto the class
            self.class.send(:define_method, message, lambda { |*largs|
              return mm.match(self, message, *largs) 
            })
            
            # Store the name of the cached method in order to facilitate
            # introspection.
            @@append_to_cached_methods.call(message)
            
            # Call the actual method
            result = self.send(message, *args)
            
            true
          end
        end
        return matched ? result : old_method_missing(message, args, &block)
      end
    
      alias_method :old_method_missing, :method_missing
      alias_method :method_missing, :match_method_missing
    end
  }
end

#reset_match_methodsObject

Allows you to delete all defined dynamic methods on a class. This permits testing.



85
86
87
# File 'lib/matches.rb', line 85

def reset_match_methods
  # @@match_methods = []
end