Class: Aspector::Advice

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

Overview

A single aspect advice representation

Constant Summary collapse

TYPES =

All available advices types that we support

%i(
  before
  before_filter
  after
  around
  raw
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, type, method_matcher, with_method, options = {}, &block) ⇒ Advice

Returns a new instance of Advice.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aspector/advice.rb', line 29

def initialize(parent, type, method_matcher, with_method, options = {}, &block)
  @type = type
  @parent = parent
  @options = options
  @advice_block = block
  @method_matcher = method_matcher
  @name = @options[:name] || "advice_#{index}"

  if with_method.is_a? Symbol
    @with_method = with_method
  else
    @advice_code = with_method
  end
end

Instance Attribute Details

#advice_blockObject (readonly)

Returns the value of attribute advice_block.



26
27
28
# File 'lib/aspector/advice.rb', line 26

def advice_block
  @advice_block
end

#advice_codeObject (readonly)

Returns the value of attribute advice_code.



26
27
28
# File 'lib/aspector/advice.rb', line 26

def advice_code
  @advice_code
end

#indexObject

Returns the value of attribute index.



27
28
29
# File 'lib/aspector/advice.rb', line 27

def index
  @index
end

#method_matcherObject (readonly)

Returns the value of attribute method_matcher.



26
27
28
# File 'lib/aspector/advice.rb', line 26

def method_matcher
  @method_matcher
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/aspector/advice.rb', line 26

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/aspector/advice.rb', line 26

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



26
27
28
# File 'lib/aspector/advice.rb', line 26

def type
  @type
end

Instance Method Details

#match?(method, context = nil) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
# File 'lib/aspector/advice.rb', line 50

def match?(method, context = nil)
  return false if method == with_method
  return false unless @method_matcher.match?(method, context)

  return true unless @options[:except]

  @except ||= MethodMatcher.new(@options[:except])

  !@except.match?(method)
end

#to_sObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aspector/advice.rb', line 65

def to_s
  s = "#{name}: "
  s << type.to_s.upcase
  s << ' [' << @method_matcher.to_s << '] DO '

  if @with_method
    s << @with_method.to_s
  else
    s << 'stuff in block'
  end
  s << ' WITH OPTIONS ' << @options.inspect
  s
end

#use_deferred_logic?(logic) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/aspector/advice.rb', line 61

def use_deferred_logic?(logic)
  method_matcher.use_deferred_logic? logic
end

#with_methodObject



44
45
46
47
48
# File 'lib/aspector/advice.rb', line 44

def with_method
  return nil if @advice_code

  @with_method ||= "aop_#{hash.abs}"
end