From eebb9ddf9ba559a510975c486fe59a4edc9da97d Mon Sep 17 00:00:00 2001 From: Patrick Robertson Date: Wed, 1 May 2013 17:10:06 -0700 Subject: Convert ActiveModel to 1.9 hash syntax. I also attempted to fix other styleguide violations such as { a: :b } over {a: :b} and foo(b: 'bar') over foo( b: 'bar' ). --- activemodel/lib/active_model/callbacks.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activemodel/lib/active_model/callbacks.rb') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index b5562dda2e..16188aec6b 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -100,10 +100,10 @@ module ActiveModel def define_model_callbacks(*callbacks) options = callbacks.extract_options! options = { - :terminator => "result == false", - :skip_after_callbacks_if_terminated => true, - :scope => [:kind, :name], - :only => [:before, :around, :after] + terminator: "result == false", + skip_after_callbacks_if_terminated: true, + scope: [:kind, :name], + only: [:before, :around, :after] }.merge!(options) types = Array(options.delete(:only)) -- cgit v1.2.3 From eb93d16e49e0582a42b9f1460c41b517dd7044b3 Mon Sep 17 00:00:00 2001 From: wangjohn Date: Wed, 8 May 2013 13:14:12 -0400 Subject: Using define method instead of class eval when defining model callbacks. Based on my benchmark results, this change improves performance substantially when defining callbacks. This benchmark (https://gist.github.com/wangjohn/5542610) was run using the current master and also using my experimental branch which replaced class_eval with define_single_method. Using class_eval (current master): user system total real 10 trials 0.000000 0.000000 0.000000 ( 0.001568) 50 trials 0.020000 0.000000 0.020000 ( 0.021715) 500 trials 0.110000 0.000000 0.110000 ( 0.115357) 1000 trials 0.250000 0.000000 0.250000 ( 0.260025) 10000 trials 2.560000 0.000000 2.560000 ( 2.568408) 50000 trials 12.800000 0.010000 12.810000 ( 12.886871) Using define_single_method (experimental branch): user system total real 10 trials 0.000000 0.000000 0.000000 ( 0.000790) 50 trials 0.000000 0.000000 0.000000 ( 0.002960) 500 trials 0.050000 0.010000 0.060000 ( 0.055690) 1000 trials 0.100000 0.000000 0.100000 ( 0.094073) 10000 trials 0.890000 0.000000 0.890000 ( 0.900364) 50000 trials 4.650000 0.000000 4.650000 ( 4.686127) --- activemodel/lib/active_model/callbacks.rb | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'activemodel/lib/active_model/callbacks.rb') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 16188aec6b..94d2181e4d 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -120,30 +120,24 @@ module ActiveModel private def _define_before_model_callback(klass, callback) #:nodoc: - klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1 - def self.before_#{callback}(*args, &block) - set_callback(:#{callback}, :before, *args, &block) - end - CALLBACK + klass.define_singleton_method("before_#{callback}") do |*args, &block| + set_callback(:"#{callback}", :before, *args, &block) + end end def _define_around_model_callback(klass, callback) #:nodoc: - klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1 - def self.around_#{callback}(*args, &block) - set_callback(:#{callback}, :around, *args, &block) - end - CALLBACK + klass.define_singleton_method("around_#{callback}") do |*args, &block| + set_callback(:"#{callback}", :around, *args, &block) + end end def _define_after_model_callback(klass, callback) #:nodoc: - klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1 - def self.after_#{callback}(*args, &block) - options = args.extract_options! - options[:prepend] = true - options[:if] = Array(options[:if]) << "value != false" - set_callback(:#{callback}, :after, *(args << options), &block) - end - CALLBACK + klass.define_singleton_method("after_#{callback}") do |*args, &block| + options = args.extract_options! + options[:prepend] = true + options[:if] = Array(options[:if]) << "value != false" + set_callback(:"#{callback}", :after, *(args << options), &block) + end end end end -- cgit v1.2.3 From ba552764344bc0a3c25b8576ec11f127ceaa16da Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 14 May 2013 16:03:09 -0700 Subject: deprecating string based terminators --- activemodel/lib/active_model/callbacks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib/active_model/callbacks.rb') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 94d2181e4d..8b09f8b203 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -100,7 +100,7 @@ module ActiveModel def define_model_callbacks(*callbacks) options = callbacks.extract_options! options = { - terminator: "result == false", + terminator: ->(_,result) { result == false }, skip_after_callbacks_if_terminated: true, scope: [:kind, :name], only: [:before, :around, :after] -- cgit v1.2.3