Class: AspectR::Aspect

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

Defined Under Namespace

Modules: AspectSupport

Constant Summary collapse

PRE =
:PRE
POST =
:POST
@@__aop_dispatch =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(never_wrap = "^$ ") ⇒ Aspect

Returns a new instance of Aspect.



29
30
31
# File 'lib/aspectr.rb', line 29

def initialize(never_wrap = "^$ ")
  @never_wrap = /^__|^send$|^object_id$|^class$|#{never_wrap}/
end

Class Method Details

.dispatch?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/aspectr.rb', line 69

def Aspect.dispatch?
  @@__aop_dispatch
end

Instance Method Details

#add_advice(target, joinpoint, method, advice) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/aspectr.rb', line 55

def add_advice(target, joinpoint, method, advice)
  prepare(target)
  if advice
	target.__aop_install_dispatcher(method)
	target.__aop_add_advice(joinpoint, method, self, advice)
  end
end

#disable_advice_dispatchingObject



73
74
75
76
77
78
79
80
# File 'lib/aspectr.rb', line 73

def disable_advice_dispatching
  begin
	@@__aop_dispatch = false
	yield
  ensure
	@@__aop_dispatch = true
  end
end

#get_methods(target, args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/aspectr.rb', line 82

def get_methods(target, args)
  if args.first.is_a? Regexp
	if target.kind_of?(Class)
	  methods = target.instance_methods(true)
	else
	  methods = target.methods
	end
	methods = methods.grep(args.first).collect{|e| e.intern}
  else
	methods = args
  end
  methods.select {|method| wrappable?(method)}
end

#prepare(target) ⇒ Object



100
101
102
103
104
105
# File 'lib/aspectr.rb', line 100

def prepare(target)
  unless target.respond_to?("__aop_init")
	target.extend AspectSupport 
	target.__aop_init
  end
end

#remove_advice(target, joinpoint, method, advice) ⇒ Object



63
64
65
# File 'lib/aspectr.rb', line 63

def remove_advice(target, joinpoint, method, advice)
  target.__aop_remove_advice(joinpoint, method, self, advice) if advice
end

#unwrap(target, pre, post, *args) ⇒ Object



40
41
42
43
44
45
# File 'lib/aspectr.rb', line 40

def unwrap(target, pre, post, *args)
  get_methods(target, args).each do |method_to_unwrap|
	remove_advice(target, PRE, method_to_unwrap, pre)
	remove_advice(target, POST, method_to_unwrap , post)
  end
end

#wrap(target, pre, post, *args) ⇒ Object



33
34
35
36
37
38
# File 'lib/aspectr.rb', line 33

def wrap(target, pre, post, *args)		
  get_methods(target, args).each do |method_to_wrap|
	add_advice(target, PRE, method_to_wrap, pre)
	add_advice(target, POST, method_to_wrap, post)
  end
end

#wrap_with_code(target, preCode, postCode, *args) ⇒ Object

Sticky and faster wrap (can’t be unwrapped).



48
49
50
51
52
53
# File 'lib/aspectr.rb', line 48

def wrap_with_code(target, preCode, postCode, *args)
  prepare(target)
  get_methods(target, args).each do |method_to_wrap|
	target.__aop_wrap_with_code(method_to_wrap, preCode, postCode)
  end
end

#wrappable?(method) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/aspectr.rb', line 96

def wrappable?(method)
  method.to_s !~ @never_wrap
end