Skip to content

Aspect Oriented Programming (AOP) for Ruby

License

Notifications You must be signed in to change notification settings

gcao/aspector

Repository files navigation

Aspector

Aspector = ASPECT Oriented Ruby programming

Deprecated

Aspector is deprecated.

We recommend Ruby native Module#prepend functionality to build up aspects.

About

Aspector allows to use aspect oriented programming with Ruby.

Aspector allows adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification.

Highlights

  • Encapsulate logic as aspects and apply to multiple targets easily
  • Support before/before_filter/after/around advices
  • Work anywhere - inside/outside the target class, before/after methods are created
  • Use regexp matching to apply advices to multiple methods
  • Small codebase, intuitive API
  • Conditional aspects disabling/enabling
  • Standarized logging API
  • Aspects are applicable to both classes/modules and instances
  • Object extensions for easier usage

Example usages

Aspector should be used whenever you have a cross-cutting concerns, especially when they don't perform any business logic. For example use can use it to provide things like:

  • Logging
  • Monitoring
  • Performance benchmarking
  • Any type of transactions wrapping
  • Events producing for systems like Apache Kafka
  • Etc...

Installation

gem install aspector

or put it inside of your Gemfile:

gem 'aspector'

Examples

To see how to use Aspector, please review examples that are in examples directory.

If you need more detailed examples, please review files in spec/functionals and spec/units/advices.

Here's a simple example how Aspector can be used:

classExampleClassdeftestputs'test'endendaspect=Aspectordotargetdodefdo_thisputs'do_this'endendbefore:test,:do_thisbefore:testdoputs'do_that'endendaspect.apply(ExampleClass)element=ExampleClass.newelement.testaspect.disable!element.test# Expected output# do_this# do_that# test# test

Configuration options

Aspector is really easy to use. After installation it doesn't require any additional configuration. You can however set two environments variables that are related to logging:

ENV variable nameDescription
ASPECTOR_LOGGERAny logger class you want to use (if you don't want to use Aspector standard logger)
ASPECTOR_LOG_LEVELDEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN

Aspector::Logger inherits from a standard Ruby logger. Log levels and the API are pretty standard. You can however use yor own:

ASPECTOR_LOGGER='MyApp::Logger'ASPECTOR_LOG_LEVEL='any log level'rubyaspected_stuff.rb

Default and apply options

Here are options that you can use when creating or applying a single aspect:

Option nameTypeDescription
exceptSymbol, String, Regexp or Array of thoseWill apply aspect to all the methods except those listed
nameStringAdvice name (really useful only for debugging)
methodsArray of Symbol, String, RegexpMethod names (or regexp for matching) to which we should apply given aspect
methodSymbol, String, Regexp or Array of thoseActs as methods but accepts a single name of method (or a single regexp)
existing_methods_onlyBoolean (true/false) - default: falseWill apply aspect only to already defined methods
new_methods_onlyBoolean (true/false) - default: falseWill apply aspect only to methods that were defined after aspect was applied
private_methodsBoolean (true/false) - default: falseShould the aspect be applied to private methods as well (public only by default)
class_methodsBoolean (true/false) - default: falseShould the aspect for instance methods of class methods of a given element
method_argBoolean (true/false) - default: falseDo we want to have access to base method arguments in the aspect method/block
interception_argBoolean (true/false) - default: falseDo we want to have access to the interception instance in the aspect method/block

Contributing to aspector

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add specs for it. This is important so I don't break it in a future version unintentionally.
  • If it is a new functionality or feature please provide examples
  • Please benchmark any functionality that might have a performance impact
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2015 Guoliang Cao, Maciej Mensfeld. See LICENSE.txt for further details.

About

Aspect Oriented Programming (AOP) for Ruby

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

close