• Stars
    star
    151
  • Rank 246,057 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Aspect Oriented Programming (AOP) for Ruby

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:

class ExampleClass
  def test
    puts 'test'
  end
end

aspect = Aspector do
  target do
    def do_this
      puts 'do_this'
    end
  end

  before :test, :do_this

  before :test do
    puts 'do_that'
  end
end

aspect.apply(ExampleClass)
element = ExampleClass.new
element.test
aspect.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 name Description
ASPECTOR_LOGGER Any logger class you want to use (if you don't want to use Aspector standard logger)
ASPECTOR_LOG_LEVEL DEBUG < 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' ruby aspected_stuff.rb

Default and apply options

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

Option name Type Description
except Symbol, String, Regexp or Array of those Will apply aspect to all the methods except those listed
name String Advice name (really useful only for debugging)
methods Array of Symbol, String, Regexp Method names (or regexp for matching) to which we should apply given aspect
method Symbol, String, Regexp or Array of those Acts as methods but accepts a single name of method (or a single regexp)
existing_methods_only Boolean (true/false) - default: false Will apply aspect only to already defined methods
new_methods_only Boolean (true/false) - default: false Will apply aspect only to methods that were defined after aspect was applied
private_methods Boolean (true/false) - default: false Should the aspect be applied to private methods as well (public only by default)
class_methods Boolean (true/false) - default: false Should the aspect for instance methods of class methods of a given element
method_arg Boolean (true/false) - default: false Do we want to have access to base method arguments in the aspect method/block
interception_arg Boolean (true/false) - default: false Do 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.

More Repositories

1

aspect4r

Aspect Oriented Programming for Ruby
Ruby
26
star
2

T.js

T - a javascript template engine
JavaScript
16
star
3

AlignText.tmbundle

Align text by regular expression pattern
Ruby
12
star
4

fffocus

JavaScript
8
star
5

stocko

a rails application that reads/processes stock data
Ruby
6
star
6

Tmux.alfredworkflow

Ruby
6
star
7

gene-new

Nim
6
star
8

discuz_robot

Discuz robot
Ruby
4
star
9

gocool

Go game repository on rails
JavaScript
4
star
10

jsgameviewer

Javascript based Game Viewer
JavaScript
4
star
11

discuz_int

Rails engine that integrate a Discuz forum with a Rails application
Ruby
3
star
12

validanguage

Validanguage, a client side validation framework by the Long Doctor
JavaScript
3
star
13

sgf_parser

A SGF Parser in ruby
Ruby
3
star
14

report_this

A gem that provides basic report functionality
Ruby
2
star
15

server_extras

Ruby
2
star
16

xml4p

XML wrapper for any project, support libxml and rexml
Ruby
2
star
17

daoqi_old

Daoqi
JavaScript
2
star
18

gene.nim

Nim
2
star
19

littlego_old

This is a clone of the littlego project.
Objective-C
2
star
20

stocko_old

A small program that reads/processes stock data.
Ruby
2
star
21

chef

Chef configuration for EC2
Ruby
1
star
22

konnect

Ruby
1
star
23

viewdget

TODO: one-line summary of your gem
Ruby
1
star
24

mblocker

Block mitbbs posts by unwelcome users
JavaScript
1
star
25

deployer

Ruby
1
star
26

bbs

PHP
1
star
27

Connect

Objective-C
1
star
28

tennis-scrap

Ruby
1
star
29

juggernaut_test

Ruby
1
star
30

gamesapp

Games CouchApp
JavaScript
1
star
31

gcao.tmbundle

A TextMate bundle which contains some convenient commands and macros I am using.
Ruby
1
star
32

cool_games

TODO: one-line summary of your gem
JavaScript
1
star
33

gene.rb

Generic Data Format
Ruby
1
star
34

gcao.github.com

CSS
1
star
35

ucenter

PHP
1
star
36

dev_setup

Store config files for dev machine
Ruby
1
star
37

viewit

Experimenting with separating data from html
JavaScript
1
star
38

juggernaut_jquery

JavaScript
1
star
39

state_maze

A simple state machine
Ruby
1
star
40

sitex

Experiment with node.js, express, jade, coffeescript, mongodb etc
CoffeeScript
1
star
41

iloveweiqi

JavaScript
1
star
42

jsgameviewer4bbs

JavaScript
1
star