Class: Aspector::Advice

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

Constant Summary collapse

BEFORE =
1
AFTER =
2
AROUND =
3
RAW =
4

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.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aspector/advice.rb', line 12

def initialize parent, type, method_matcher, with_method, options = {}, &block
  @parent         = parent
  @type           = type
  @method_matcher = method_matcher

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

  @options        = options
  @advice_block   = block
end

Instance Attribute Details

#advice_blockObject (readonly)

Returns the value of attribute advice_block.



9
10
11
# File 'lib/aspector/advice.rb', line 9

def advice_block
  @advice_block
end

#advice_codeObject (readonly)

Returns the value of attribute advice_code.



9
10
11
# File 'lib/aspector/advice.rb', line 9

def advice_code
  @advice_code
end

#indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/aspector/advice.rb', line 10

def index
  @index
end

#method_matcherObject (readonly)

Returns the value of attribute method_matcher.



9
10
11
# File 'lib/aspector/advice.rb', line 9

def method_matcher
  @method_matcher
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/aspector/advice.rb', line 9

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/aspector/advice.rb', line 9

def type
  @type
end

Instance Method Details

#after?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/aspector/advice.rb', line 56

def after?
  type == AFTER
end

#around?Boolean

Returns:

  • (Boolean)


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

def around?
  type == AROUND
end

#before?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/aspector/advice.rb', line 52

def before?
  type == BEFORE
end

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

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/aspector/advice.rb', line 37

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

  return true unless @options[:except]

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

  not @except.match?(method)
end

#nameObject



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

def name
  @options[:name] || "advice #{index}"
end

#raw?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/aspector/advice.rb', line 48

def raw?
  type == RAW
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aspector/advice.rb', line 78

def to_s
  s = "#{name}: "
  s << type_name
  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

#type_nameObject



64
65
66
67
68
69
70
71
72
# File 'lib/aspector/advice.rb', line 64

def type_name
  case @type
  when BEFORE then @options[:skip_if_false] ? "BEFORE_FILTER" : "BEFORE"
  when AFTER  then "AFTER"
  when AROUND then "AROUND"
  when RAW    then "RAW"
  else "UNKNOWN?!"
  end
end

#use_deferred_logic?(logic) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/aspector/advice.rb', line 74

def use_deferred_logic? logic
  method_matcher.use_deferred_logic? logic
end

#with_methodObject



31
32
33
34
35
# File 'lib/aspector/advice.rb', line 31

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