metaprogramming - Naive aspect implementation in ruby -


i trying make simplistic implementation of aop in ruby. able implement before , after advices, got stuck around advice.

this target class going advised:

class myclass   def method     puts "running method"   end end 

this aspect class instantiate objects capable of making advices:

class aspect   def advise(class_name, method, type, &block)     class_name.send(:alias_method, :proceed, :method)     class_name.send(:define_method, :method)       case type       when :before         yield         proceed       when :after         proceed         yield       when :around         yield(proceed) # * proceed old version of method       end     end   end end 

(*) yield should execute block around myclass#proceed on current object when method invoked.

creating target , aspect:

mc = myclass.new = aspect.new()  

invoking method without advising it:

puts mc.method 

advising myclass#method around:

a.advise(myclass, :method, :around) |proceed|   puts "first"   proceed # not working *   puts "last" end  puts mc.method 

(*) not being able pass identify call of proceed, invocation of old method without advice.

the output should be:

first running method last 

in ruby, method call looks this:

receiver.method(arguments) 

or, can leave off receiver if receiver self.

so, call method named proceed on receiver, write

receiver.proceed 

however, in implementation, don't keep track of receiver should be, since don't know receiver, cannot call method.

note there lots of other problems approach well. example, if advise multiple methods, alias them same method, overwriting each other.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -