Skip to content
This repository
Browse code

AS::Callbacks: Refactor :per_key option

  • Loading branch information...
commit a21b5771ad6673b8743f76d0f883f37bdf11ce4c 1 parent 5f8274e
Bogdan Gusiev authored January 17, 2012
42  activesupport/lib/active_support/callbacks.rb
@@ -91,29 +91,23 @@ def halted_callback_hook(filter)
91 91
     class Callback #:nodoc:#
92 92
       @@_callback_sequence = 0
93 93
 
94  
-      attr_accessor :chain, :filter, :kind, :options, :per_key, :klass, :raw_filter
  94
+      attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter
95 95
 
96 96
       def initialize(chain, filter, kind, options, klass)
97 97
         @chain, @kind, @klass = chain, kind, klass
98 98
         normalize_options!(options)
99 99
 
100  
-        @per_key              = options.delete(:per_key)
101 100
         @raw_filter, @options = filter, options
102 101
         @filter               = _compile_filter(filter)
103 102
         @compiled_options     = _compile_options(options)
104 103
         @callback_id          = next_id
105  
-
106  
-        _compile_per_key_options
107 104
       end
108 105
 
109 106
       def clone(chain, klass)
110 107
         obj                  = super()
111 108
         obj.chain            = chain
112 109
         obj.klass            = klass
113  
-        obj.per_key          = @per_key.dup
114 110
         obj.options          = @options.dup
115  
-        obj.per_key[:if]     = @per_key[:if].dup
116  
-        obj.per_key[:unless] = @per_key[:unless].dup
117 111
         obj.options[:if]     = @options[:if].dup
118 112
         obj.options[:unless] = @options[:unless].dup
119 113
         obj
@@ -124,8 +118,9 @@ def normalize_options!(options)
124 118
         options[:unless] = Array(options[:unless])
125 119
 
126 120
         options[:per_key] ||= {}
127  
-        options[:per_key][:if] = Array(options[:per_key][:if])
128  
-        options[:per_key][:unless] = Array(options[:per_key][:unless])
  121
+
  122
+        options[:if] += Array(options[:per_key][:if])
  123
+        options[:unless] += Array(options[:per_key][:unless])
129 124
       end
130 125
 
131 126
       def name
@@ -147,22 +142,11 @@ def _update_filter(filter_options, new_options)
147 142
 
148 143
       def recompile!(_options, _per_key)
149 144
         _update_filter(self.options, _options)
150  
-        _update_filter(self.per_key, _per_key)
  145
+        _update_filter(self.options, _per_key)
151 146
 
152 147
         @callback_id      = next_id
153 148
         @filter           = _compile_filter(@raw_filter)
154 149
         @compiled_options = _compile_options(@options)
155  
-                            _compile_per_key_options
156  
-      end
157  
-
158  
-      def _compile_per_key_options
159  
-        key_options  = _compile_options(@per_key)
160  
-
161  
-        @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
162  
-          def _one_time_conditions_valid_#{@callback_id}?
163  
-            true if #{key_options}
164  
-          end
165  
-        RUBY_EVAL
166 150
       end
167 151
 
168 152
       # Wraps code with filter
@@ -200,11 +184,6 @@ def apply(code, key=nil, object=nil)
200 184
         end
201 185
       end
202 186
 
203  
-
204  
-      def one_time_conditions_valid?(object)
205  
-        object.send("_one_time_conditions_valid_#{@callback_id}?")
206  
-      end
207  
-
208 187
       private
209 188
 
210 189
       # Compile around filters with conditions into proxy methods
@@ -341,7 +320,7 @@ def compile(key=nil, object=nil)
341 320
         method << "halted = false"
342 321
 
343 322
         callbacks = yielding
344  
-        applicable_callbacks_for(key, object).reverse_each do |callback|
  323
+        reverse_each do |callback|
345 324
           callbacks = callback.apply(callbacks, key, object)
346 325
         end
347 326
         method << callbacks
@@ -369,13 +348,6 @@ def yielding
369 348
         method.join("\n")
370 349
       end
371 350
 
372  
-      # Selects callbacks that have valid <tt>:per_key</tt> condition
373  
-      def applicable_callbacks_for(key, object)
374  
-        return self unless key
375  
-        select do |callback|
376  
-          callback.one_time_conditions_valid?(object)
377  
-        end
378  
-      end
379 351
     end
380 352
 
381 353
     module ClassMethods
@@ -402,7 +374,7 @@ def __reset_runner(symbol)
402 374
       end
403 375
 
404 376
       def __callback_runner_name(key, kind)
405  
-        "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks"
  377
+        "_run__#{self.name.hash.abs}__#{kind}__callbacks"
406 378
       end
407 379
 
408 380
       # This is used internally to append, prepend and skip callbacks to the

0 notes on commit a21b577

Please sign in to comment.
Something went wrong with that request. Please try again.