Module: Aspekt::Helpers::ShorthandMethods

Defined in:
lib/aspekt/helpers/shorthand_methods.rb

Instance Method Summary collapse

Instance Method Details

#after(*pointcuts, &block) ⇒ Object

Shorthand for after advice.

Examples

after type: Test, methods: /to_/ do
  puts "Before called on a object #{self}"
end

class TestMe
  after methods: [:a, /b/] do |joinpoint|   # note how type or instance is not specified, it is automatically resolved to self (what is TestMe in class body)
    puts "After #{joinpoint}!"
  end
end


47
48
49
# File 'lib/aspekt/helpers/shorthand_methods.rb', line 47

def after *pointcuts, &block
  Aspekt::Advice.new type: :after, pointcuts: pointcuts, &block
end

#around(*pointcuts, &block) ⇒ Object

Shorthand for around advice.

Example

around type: Test, methods: /to_/ do |joinpoint|
  puts "Before called on a object #{self} and method #{joinpoint[:method]}"
  result = joinpoint.proceed
  puts "After called on a object #{self} and method #{joinpoint[:method]}"
  result
end

class TestMe
  around methods: [:a, /b/] do |joinpoint|   # note how type or instance is not specified, it is automatically resolved to self (what is TestMe in class body)
    puts "Before around #{joinpoint}!"
    result = joinpoint.proceed
    puts "After around #{joinpoint}!"
    result 
  end
end


73
74
75
# File 'lib/aspekt/helpers/shorthand_methods.rb', line 73

def around *pointcuts, &block
  Aspekt::Advice.new type: :around, pointcuts: pointcuts, &block
end

#before(*pointcuts, &block) ⇒ Object

Shorthand for before advice.

Examples

before type: Test, methods: /to_/ do
  puts "Before called on a object #{self}"
end

before(
        {type: Class1, method: :method1},
        {types: [Class2, Class3], methods: [/cotaining/, :method3]},
        {instances: [Class3, Class4, some_object], method: :lal}
       ) do |joinpoint|    
  puts "Before #{joinpoint[:method]} on #{self}. Other information of joinpoint: #{joinpoint}."
end

class TestMe
  before methods: [:a, /b/] do |joinpoint|   # note how type or instance is not specified, it is automatically resolved to self (what is TestMe in class body)
    puts "Before #{joinpoint}!"
  end
end


27
28
29
# File 'lib/aspekt/helpers/shorthand_methods.rb', line 27

def before *pointcuts, &block
  Aspekt::Advice.new type: :before, pointcuts: pointcuts, &block
end