Class: Aspekt::Pointcut

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

Instance Method Summary collapse

Methods inherited from Object

all

Constructor Details

#initialize(*matchers) ⇒ Pointcut

Example usage

class SomeClass

  def self.some_method
    return "some_value_from_singleton"
  end

  def some_method
    return "some_value"
  end

  def other_method
    return "other_value"
  end

end

some_object = SomeClass.new

Pointcuts for instance with types

pointcut = Aspekt::Pointcut.new( class: :SomeClass, method: :some_method )
pointcut = Aspekt::Pointcut.new( class: SomeClass, method: /some/ )
pointcut = Aspekt::Pointcut.new( class: /Some/, method: /some/ )

Pointcut for class method (singleton method)

pointcut = Aspekt::Pointcut.new( object: SomeClass, method: :some_method )
pointcut = Aspekt::Pointcut.new( object: /Some/, method: /some/)
pointcut = Aspekt::Pointcut.new( class: (class SomeClass; class<<self; self; end; end, method: :some_method )

Pointcut for only one object

pointcut = Aspekt::Pointcut.new( object: some_object, method: :some_method)

Arguments

  • object: object, method: :method

  • [object, method: :method, object2, method: :method2

  • objects: [object, object2], methods: [:method, :method2]

  • class: :SomeClass, methods: [:method, :method2]

  • {objects: [Hash, Array], method: new, Array, method: :push, [Hash, Array, SomeClass], method: /_id/

Limitations

Instances of Regexp, Symbol and Constant are used as finders not thought of as objects. TODO: Fix. Possibly add a new hash key value?



75
76
77
78
79
80
81
82
# File 'lib/aspekt.rb', line 75

def initialize *matchers 
  matchers = matchers.flatten.each do |matcher|
    matcher[:classes] = [matcher.delete(:class)].flatten if matcher.has_key?(:class)
    matcher[:objects] = [matcher.delete(:object)].flatten if matcher.has_key?(:object)
    matcher[:methods] = [matcher.delete(:method)].flatten if matcher.has_key?(:method)
  end
  super matchers: matchers
end

Instance Method Details

#is_matching?(opts) ⇒ Boolean

Example usage

pointcut = Aspekt::Pointcut.new( class: :SomeClass, method: :some_method ).is_matching?( object: SomeClass.new )
pointcut = Aspekt::Pointcut.new( object: :SomeClass, method: :some_method ).is_matching?( object: SomeClass )

Arguments

  • object: object

  • object: object, method: :method

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/aspekt.rb', line 91

def is_matching? opts
  raise ArgumentError, "has to include keys 'object', may include 'method'." unless opts[:object]

  self[:matchers].each do |matcher|
    # object matching
    return true if
    (matcher.has_key(:objects) and matcher[:objects].select{|object| object.is_same?(opts[:object])}.length > 0) or
    (matcher.has_key(:classes) and matcher[:classes].select{|klass| klass.is_same?(opts[:object].class.name)}.length > 0)
    # method matching
    return true unless opts.has_key?(:methods) or
    (matcher.has_key(:methods) and matcher[:methods].select{|klass| klass.is_same?(opts[:methods].class.name)}.length > 0)
  end

  return false
end