module AspectR::Aspect::AspectSupport

Public Instance Methods

__aop_add_advice(joinpoint, method, aspect, advice) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_init() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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