Project

aop

0.0
No commit activity in last 3 years
No release in over 3 years
Thin and fast framework for Aspect Oriented Programming in Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 10.0
~> 1.7
 Project Readme

Aop

Build Status

Very thin AOP gem for Ruby.

Thin and fast framework for Aspect Oriented Programming in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'aop'

And then execute:

$ bundle

Or install it yourself as:

$ gem install aop

Usage

Before advice

module Authentication
  Aop["BankAccount#transfer:before"].advice do |account, *args, &blk|
    can!(:transfer, account)
  end

  def self.can!(action, subject)
    # raises error if current user can't execute `action` on `subject`
  end
end

After advice

module Analytics
  Aop["User#sign_in:after"].advice do |target, *args, &blk|
    report("sign_in", user.id)
  end
end

Around advice

module Transactional
  Aop["BankAccount#transfer:around"].advice do |joint_point, account, *args, &blk|
    start_transaction
    joint_point.call
    finish_transaction
  end
end

Class methods

Use .method notation:

module Transactional
  Aop["BankAccount.transfer:around"].advice do |joint_point, klass, *args, &blk|
    start_transaction
    joint_point.call
    finish_transaction
  end
end

Multiple classes, methods and advices

Use , to use multiple classes, methods and advices:

module Analytics
  Aop["User,Admin#sign_in,.sign_up,#sign_out:before,:after"].advice do |target, *args, &blk|
    report("auth_action", user.id)
  end
end

Handling missed pointcuts

When pointcut is gone, for example when method or class gets renamed, it is a potential bug, because some code will not be run. This library tackles this problem by failing hard when pointcut can not be found.

Example:

Aop["Admin#sign_in:after"].advice do |target, *args, &blk|
  # .. do something ..
end

Then somebody renames Admin#sign_in to Admin#logout, and when you run the code you will get:

Aop::PointcutNotFound: Unable to find pointcut Admin#sign_in
    Reason: #<NameError: undefined method `sign_in' for class `Admin'>
    .. backtrace ..

Contributing

  1. Fork it ( https://github.com/waterlink/aop/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request