Class: MethodFound::Interceptor

Inherits:
Module
  • Object
show all
Defined in:
lib/method_found/interceptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regex = nil, &intercept_block) ⇒ Interceptor



5
6
7
# File 'lib/method_found/interceptor.rb', line 5

def initialize(regex = nil, &intercept_block)
  define_method_missing(regex, &intercept_block) unless regex.nil?
end

Instance Attribute Details

#regexObject (readonly)

Returns the value of attribute regex.



3
4
5
# File 'lib/method_found/interceptor.rb', line 3

def regex
  @regex
end

Instance Method Details

#define_method_missing(regex, &intercept_block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/method_found/interceptor.rb', line 9

def define_method_missing(regex, &intercept_block)
  @regex = regex
  define_method :method_missing do |method_name, *arguments, &method_block|
    if matches = regex.match(method_name)
      instance_exec(method_name, matches, *arguments, &intercept_block)
    else
      super(method_name, *arguments, &method_block)
    end
  end

  define_method :respond_to_missing? do |method_name, include_private = false|
    (method_name =~ regex) || super(method_name, include_private)
  end
end

#inspectObject



24
25
26
27
28
# File 'lib/method_found/interceptor.rb', line 24

def inspect
  klass = self.class
  name  = klass.name || klass.inspect
  "#<#{name}: #{regex.inspect}>"
end