metaprogramming - Cross-cutting logging in Ruby -


I'm trying to add logging to a method from the outside (aspect-oriented style)

  puts square AF test "I'm doing something ..." with end and class A # logging! Alias_method: test_orig ,: test def test "log message!" The test_orig end end a = ANU a.test  

The above work is fine, except that if I ever need to nick the method, then it goes into an infinite loop is. I want something more super, where I can expand it according to its requirement, and with its expansion its parent parent

using another option unbound method:

  class is a original_test = Instance_method (: test) define_method (: test) "log messages!" Original_test.bind (self) .call end end class A original_test = instance_method (: test) counter = 0 define_method (: test) puts counter + = 1 "counter = # {counter}" original_test.bind (self) Kcol end End IRB & gt; A.new.test counter = 1 log message! # = & Gt; # .... irb & gt; A.new.test counter = 2 log messages! # = & Gt; # .....  

The advantage of this is that it does not pollute the namespace with the names of the additional method, and becomes quite smooth, if you have a class method Want to create Add_logging or what do you have.

  class module def add_logging (* method_names) method_names.each do | Method_name | Original_maximum = example_date (method_name) defined_math (method_name). * Args, and Black | "Logging # {method_name}" original_method.bind (self) Kcol (* Arges, and black) and and and and Class A add_logging: Test and  

Or, if you'd like boiler plate To be able to do a lot of aspects / oa, you can write a method that writes aspects of add-on!

  class module def self.define_aspect (aspect_name, & definition) define_method (: "add _ # {aspect_name}") | * Method_names | | Do Method_names.each | Method_name | Mul_mahim = Udahrn_anupryog (Vidhi_nav) defined _mthi (Vidhi_nav, and (by definition [method_name, original_method]) end end end # to Pribhashit_saptekt a add_logging method: Do lambda logging | method_name, original_method | | * Arges, and Black | "Logging # { method_name} "original_method.bind (self) .call (* args, and blk) end end # make a add_counting method global_counter = 0 define_aspect: counting do | method_name, original_method | Local_counter = 0 Lambada to | * Arges, and Black | Global_counter + = 1 local_counter + = 1 puts "counter: global @ # {global_counter}, local @ # {local_ Counter} "Original_math. Bind (self). Call (* Args, and Black) And End And Class A Df Test" I'm doing something ... "End DEF test1 says," I'm doing something once. .. "End Def Test 2 says," I'm doing something twice ... "," I am doing something again ... "End DEF Test 3 puts" I'm tripling .. . "Say," I'm doing something for all three ... "puts" I'm doing something with all three ... " Says Y_tasts, "I'm doing something else ..." Finally add_logging: test ,: test2 ,: test3 add_counting: other_tests ,: test1 ,: test3 end  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -