Class: Multidispatch::Interceptor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = nil) ⇒ Interceptor

Returns a new instance of Interceptor.



4
5
6
# File 'lib/multidispatch/interceptor.rb', line 4

def initialize(store = nil)
  @store = store
end

Instance Attribute Details

#storeObject

Returns the value of attribute store.



2
3
4
# File 'lib/multidispatch/interceptor.rb', line 2

def store
  @store
end

Instance Method Details

#intercept(method) ⇒ Object



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

def intercept(method)
  return if @store.locked? # prevent recursion

  store = @store
  store.set(method.owner, method.name)
  store.lock!

  method_body = lambda do |*args|
    method = store.get(self.class, method.name, args.count)
    method.bind(self).call(*args)
  end

  method.owner.class_eval { define_method method.name, method_body }
  store.release!
end