Module: Wool::Advice

Included in:
Warning
Defined in:
lib/wool/advice/advice.rb,
lib/wool/advice/comment_advice.rb

Overview

This module provides other modules the ability to add advice to methods. This module makes use of these functions opt-in.

Defined Under Namespace

Modules: CommentAdvice

Instance Method Summary collapse

Instance Method Details

#advice_counterObject



5
6
7
# File 'lib/wool/advice/advice.rb', line 5

def advice_counter
  @advice_counter ||= 0
end

#after_advice(meth, advice) ⇒ Object



17
18
19
# File 'lib/wool/advice/advice.rb', line 17

def after_advice(meth, advice)
  with_advice(meth, :after => proc { send(advice) })
end

#argument_advice(meth, argument_tweaker) ⇒ Object



21
22
23
# File 'lib/wool/advice/advice.rb', line 21

def argument_advice(meth, argument_tweaker)
  with_advice(meth, :args => argument_tweaker)
end

#before_advice(meth, advice) ⇒ Object



13
14
15
# File 'lib/wool/advice/advice.rb', line 13

def before_advice(meth, advice)
  with_advice(meth, :before => proc { send(advice) })
end

#bump_advice_counter!Object



9
10
11
# File 'lib/wool/advice/advice.rb', line 9

def bump_advice_counter!
  @advice_counter += 1
end

#with_advice(meth, settings) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wool/advice/advice.rb', line 25

def with_advice(meth, settings)
  counter = advice_counter
  alias_method "#{meth}_old#{counter}".to_sym, meth
  define_method meth do |*args|
    identity = proc {|*x| x}
    instance_eval(&(settings[:before] || identity))
    if settings[:args]
      new_args = instance_eval(& proc { send(settings[:args], *args)})
    end
    result = send("#{meth}_old#{counter}", *new_args)
    instance_eval(&(settings[:after] || identity))

    result
  end
  bump_advice_counter!
end