Module Advisable
In: lib/more/facets/advisable.rb

Advisable

Advisable provides a means of using before, after and around adivce.

Methods

Classes and Modules

Module Advisable::Method

Public Instance methods

[Source]

# File lib/more/facets/advisable.rb, line 36
  def advice_after
    @advice_after ||= {} #Hash.new{|h,k| h[k] = []}
  end

[Source]

# File lib/more/facets/advisable.rb, line 40
  def advice_around
    @advice_around ||= {} #Hash.new{|h,k| h[k] = []}
  end

[Source]

# File lib/more/facets/advisable.rb, line 32
  def advice_before
    @advice_before ||= {} #Hash.new{|h,k| h[k] = []}
  end

Advise a method.

[Source]

# File lib/more/facets/advisable.rb, line 64
  def advise(meth)
    #return false if defined?(meth_origin)
    args = instance_method(meth).arguments

    module_eval("alias_method '\#{meth}_origin', '\#{meth}'\ndef \#{meth}(\#{args})\ntarget = method!('\#{meth}_origin')\n\nunless target.advised\nancs = self.class.ancestors.select{ |anc| anc.respond_to?(:advice_before) }\ntarget.advice_before = ancs.collect{ |anc| anc.advice_before[:'\#{meth}'] }\ntarget.advice_after  = ancs.collect{ |anc| anc.advice_after[:'\#{meth}'] }\ntarget.advice_around = ancs.collect{ |anc| anc.advice_around[:'\#{meth}'] }\ntarget.advised = true\nend\n\ntarget.call_with_advice(self, *[\#{args}])\nend\n", __FILE__, __LINE__)
  end

[Source]

# File lib/more/facets/advisable.rb, line 50
  def after(meth, &block)
    name = "#{meth}:after#{block.object_id}"
    define_method(name, &block)
    (advice_after[meth.to_sym] ||= []) << name
  end

[Source]

# File lib/more/facets/advisable.rb, line 56
  def around(meth, &block)
    name = "#{meth}:around#{block.object_id}"
    define_method(name, &block)
    (advice_around[meth.to_sym] ||= []) << name
  end

[Source]

# File lib/more/facets/advisable.rb, line 44
  def before(meth, &block)
    name = "#{meth}:before#{block.object_id}"
    define_method(name, &block)
    (advice_before[meth.to_sym] ||= []) << name
  end

after :method_added do |meth|

  advise(meth) unless defined?("#{meth}_orig")

end

[Source]

# File lib/more/facets/advisable.rb, line 93
  def method_added(meth)
    return if meth == :method_added
    @added_stack ||= []
    return if @added_stack.last == meth
    return if /_(origin)$/ =~ meth.to_s
    return if /:(before|after|around)/ =~ meth.to_s
    @added_stack << meth
    #return if instance_methods(false).include?("#{meth}_orig")
    advise(meth)
    @added_stack.pop
  end

[Validate]