Class: Aspekt::Advice

Inherits:
Object
  • Object
show all
Defined in:
lib/aspekt/advice.rb

Instance Method Summary collapse

Methods inherited from Object

all

Constructor Details

#initialize(opts, &block) ⇒ Advice

Creates new Advice Object

Example usage

advice = Aspekt::Advice.new type: :before, pointcut: pointcut do |joinpoint|
  puts "before #{joinpoint}"
end

advice = Aspekt::Advice.new type: :around, pointcuts: [pointcut1, pointcut2] do |joinpoint|
  puts "around :start for #{joinpoint}"
  return_value = joinpoint.proceed
  puts "around :end for #{joinpoint}"
end

Parameters:

  • (:type => :before|:after|:around, :pointcut|:pointcuts => Pointcut.new(...)|[Pointcut.new(...)])

Raises:

  • (ArgumentError)


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

def initialize opts, &block
  raise ArgumentError, "type has to be Symbol: before, after or around, but got '#{opts[:type]}'" unless [:before, :after, :around].include?(opts[:type])
  raise ArgumentError, "has to define at least one Pointcut with :pointcut or :pointcuts" unless opts.has_key?(:pointcut) or opts.has_key?(:pointcuts)

  # get the module in which advice was created
  begin
    opts[:module] = caller.including_same(/<module:/).collect {|l| /<module:(.*)>/.match(l)[1] }.reverse.inject(Object) {|a, b| a.const_get(b) }
  rescue
  end

  # add block info to opts
  opts[:proc] = block

  # register opts values
  super

  # weave into the code
  weave &block
end