Module: AspectR::Aspect::AspectSupport

Defined in:
lib/aspectr.rb

Defined Under Namespace

Modules: ClassSupport, InstanceSupport

Instance Method Summary collapse

Instance Method Details

#__aop_add_advice(joinpoint, method, aspect, advice) ⇒ Object



129
130
131
# File 'lib/aspectr.rb', line 129

def __aop_add_advice(joinpoint, method, aspect, advice)
	__aop_advice_list(joinpoint, method) << [aspect, advice]	
end

#__aop_advice_call_syntax(joinpoint, method, args) ⇒ Object



173
174
175
# File 'lib/aspectr.rb', line 173

def __aop_advice_call_syntax(joinpoint, method, args)
	"#{__aop_target}.__aop_call_advice(:#{joinpoint}, '#{method}', self, exit_status#{args.length>0 ? ',' + args : ''})"
end

#__aop_advice_list(joinpoint, method) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/aspectr.rb', line 118

def __aop_advice_list(joinpoint, method)
	method = method.to_s
	unless (method_hash = @__aop_advice_methods[joinpoint])
	  method_hash = @__aop_advice_methods[joinpoint] = {}
	end
	unless (advice_list = method_hash[method])
	  advice_list =  method_hash[method] = []
	end
	advice_list
end

#__aop_call_advice(joinpoint, method, *args) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/aspectr.rb', line 142

def __aop_call_advice(joinpoint, method, *args)
	__aop_advice_list(joinpoint, method).each do |aspect, advice|
	  begin
	    aspect.send(advice, method, *args)
	  rescue Exception
	    a = $!
	    raise AspectRException, "#{a.class} '#{a}' in advice #{advice}"
	  end
	end
end

#__aop_generate_args(method) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/aspectr.rb', line 153

def __aop_generate_args(method)
	arity = __aop_class.instance_method(method).arity
	if arity < 0
	  args = (0...(-1-arity)).to_a.collect{|i| "a#{i}"}.join(",")
	  args += "," if arity < -1
	  args + "*args,&block"
	elsif arity != 0
	  ((0...arity).to_a.collect{|i| "a#{i}"} + ["&block"]).join(",")
	else
	  "&block" # could be a yield in there...
	end
end

#__aop_generate_syntax(method) ⇒ Object



166
167
168
169
170
171
# File 'lib/aspectr.rb', line 166

def __aop_generate_syntax(method)
	args = __aop_generate_args(method)
	mangled_method = __aop_mangle(method)
	call = "send(\"#{mangled_method}\".to_sym, #{args})"
	return args, call, mangled_method
end

#__aop_initObject



109
110
111
112
113
114
115
116
# File 'lib/aspectr.rb', line 109

def __aop_init
	if self.is_a? Class
	  extend ClassSupport
	else
	  extend InstanceSupport
	end
	@__aop_advice_methods = {}
end

#__aop_install_dispatcher(method) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/aspectr.rb', line 177

def __aop_install_dispatcher(method)
	args, call, mangled_method = __aop_generate_syntax(method)      
	return if __aop_private_methods.include? mangled_method
	new_method = """
	  def #{method}(#{args})
	    return (#{call}) unless Aspect.dispatch?
	    begin
exit_status = nil
#{__aop_advice_call_syntax(PRE, method, args)}
exit_status = #{call}
return exit_status
	    rescue Exception
exit_status = true
raise
	    ensure
#{__aop_advice_call_syntax(POST, method, args)}
	    end
	  end
	"""	
	__aop_alias(mangled_method, method)
  __aop_eval(new_method)
end

#__aop_remove_advice(joinpoint, method, aspect, advice) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/aspectr.rb', line 133

def __aop_remove_advice(joinpoint, method, aspect, advice)
	__aop_advice_list(joinpoint, method).delete_if do |asp, adv| 
	  asp == aspect && adv == advice
	end
	# Reinstall original method if there are no advices left for this meth!
	# - except that then we could have problems with singleton instances
	# of this class? see InstanceSupport#aop_alias... /AB
end

#__aop_wrap_with_code(method, preCode, postCode) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/aspectr.rb', line 200

def __aop_wrap_with_code(method, preCode, postCode)
	args, call, mangled_method = __aop_generate_syntax(method)      
	return if __aop_private_methods.include? mangled_method
	comma = args != "" ? ", " : ""
	preCode.gsub!('INSERT_ARGS', comma + args)
	postCode.gsub!('INSERT_ARGS', comma + args)
	new_method = """
	  def #{method}(#{args})
	    #{preCode}
	    begin
 #{call}
	    ensure
 #{postCode}
	    end
	  end
	"""
	__aop_alias(mangled_method, method)
  __aop_eval(new_method)
end