Class: Schmetterling::Around

Inherits:
Object
  • Object
show all
Defined in:
lib/schmetterling/around.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, method, advice = nil, &block) ⇒ Around

Returns a new instance of Around.



4
5
6
7
# File 'lib/schmetterling/around.rb', line 4

def initialize(klass, method, advice=nil, &block)
  advice ||= block
  klass.send(:prepend, build_module_for_advice(method, advice))
end

Instance Method Details

#build_module_for_advice(adviced_method, advice) ⇒ Object



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

def build_module_for_advice(adviced_method, advice)
  Module.new do
    define_method adviced_method do |*args|
      original_method = ->(args_for_original=args) { super(args_for_original) }
      advice.call(original_method, self, *args)
    end
  end
end