Skip to content
This repository
Browse code

bye bye extlib_inheritable_*, AS callbacks now using class_attribute

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit ae44bf7c7ebb39d9a12d5253016f92617bee79c7 1 parent 43e2e10
Josh Kalderimis authored November 18, 2010 josevalim committed November 18, 2010
22  activesupport/lib/active_support/callbacks.rb
... ...
@@ -1,6 +1,6 @@
1 1
 require 'active_support/descendants_tracker'
2 2
 require 'active_support/core_ext/array/wrap'
3  
-require 'active_support/core_ext/class/inheritable_attributes'
  3
+require 'active_support/core_ext/class/attribute'
4 4
 require 'active_support/core_ext/kernel/reporting'
5 5
 require 'active_support/core_ext/kernel/singleton_class'
6 6
 
@@ -437,7 +437,7 @@ def __update_callbacks(name, filters = [], block = nil) #:nodoc:
437 437
 
438 438
         ([self] + ActiveSupport::DescendantsTracker.descendants(self)).each do |target|
439 439
           chain = target.send("_#{name}_callbacks")
440  
-          yield chain, type, filters, options
  440
+          yield target, chain.dup, type, filters, options
441 441
           target.__define_runner(name)
442 442
         end
443 443
       end
@@ -473,7 +473,7 @@ def __update_callbacks(name, filters = [], block = nil) #:nodoc:
473 473
       def set_callback(name, *filter_list, &block)
474 474
         mapped = nil
475 475
 
476  
-        __update_callbacks(name, filter_list, block) do |chain, type, filters, options|
  476
+        __update_callbacks(name, filter_list, block) do |target, chain, type, filters, options|
477 477
           mapped ||= filters.map do |filter|
478 478
             Callback.new(chain, filter, type, options.dup, self)
479 479
           end
@@ -483,6 +483,8 @@ def set_callback(name, *filter_list, &block)
483 483
           end
484 484
 
485 485
           options[:prepend] ? chain.unshift(*(mapped.reverse)) : chain.push(*mapped)
  486
+
  487
+          target.send("_#{name}_callbacks=", chain)
486 488
         end
487 489
       end
488 490
 
@@ -493,7 +495,7 @@ def set_callback(name, *filter_list, &block)
493 495
       #   end
494 496
       #
495 497
       def skip_callback(name, *filter_list, &block)
496  
-        __update_callbacks(name, filter_list, block) do |chain, type, filters, options|
  498
+        __update_callbacks(name, filter_list, block) do |target, chain, type, filters, options|
497 499
           filters.each do |filter|
498 500
             filter = chain.find {|c| c.matches?(type, filter) }
499 501
 
@@ -505,6 +507,7 @@ def skip_callback(name, *filter_list, &block)
505 507
 
506 508
             chain.delete(filter)
507 509
           end
  510
+          target.send("_#{name}_callbacks=", chain)
508 511
         end
509 512
       end
510 513
 
@@ -514,12 +517,14 @@ def reset_callbacks(symbol)
514 517
         callbacks = send("_#{symbol}_callbacks")
515 518
 
516 519
         ActiveSupport::DescendantsTracker.descendants(self).each do |target|
517  
-          chain = target.send("_#{symbol}_callbacks")
  520
+          chain = target.send("_#{symbol}_callbacks").dup
518 521
           callbacks.each { |c| chain.delete(c) }
  522
+          target.send("_#{symbol}_callbacks=", chain)
519 523
           target.__define_runner(symbol)
520 524
         end
521 525
 
522  
-        callbacks.clear
  526
+        self.send("_#{symbol}_callbacks=", callbacks.dup.clear)
  527
+
523 528
         __define_runner(symbol)
524 529
       end
525 530
 
@@ -589,9 +594,8 @@ def reset_callbacks(symbol)
589 594
       def define_callbacks(*callbacks)
590 595
         config = callbacks.last.is_a?(Hash) ? callbacks.pop : {}
591 596
         callbacks.each do |callback|
592  
-          extlib_inheritable_reader("_#{callback}_callbacks") do
593  
-            CallbackChain.new(callback, config)
594  
-          end
  597
+          class_attribute "_#{callback}_callbacks"
  598
+          send("_#{callback}_callbacks=", CallbackChain.new(callback, config))
595 599
           __define_runner(callback)
596 600
         end
597 601
       end
83  activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb
@@ -169,86 +169,3 @@ def inherited_with_inheritable_attributes(child)
169 169
     alias inherited_without_inheritable_attributes inherited
170 170
     alias inherited inherited_with_inheritable_attributes
171 171
 end
172  
-
173  
-class Class
174  
-  # Defines class-level inheritable attribute reader. Attributes are available to subclasses,
175  
-  # each subclass has a copy of parent's attribute.
176  
-  #
177  
-  # @param *syms<Array[#to_s]> Array of attributes to define inheritable reader for.
178  
-  # @return <Array[#to_s]> Array of attributes converted into inheritable_readers.
179  
-  #
180  
-  # @api public
181  
-  #
182  
-  # @todo Do we want to block instance_reader via :instance_reader => false
183  
-  # @todo It would be preferable that we do something with a Hash passed in
184  
-  #   (error out or do the same as other methods above) instead of silently
185  
-  #   moving on). In particular, this makes the return value of this function
186  
-  #   less useful.
187  
-  def extlib_inheritable_reader(*ivars, &block)
188  
-    options = ivars.extract_options!
189  
-
190  
-    ivars.each do |ivar|
191  
-      self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
192  
-        def self.#{ivar}
193  
-          return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
194  
-          ivar = superclass.#{ivar}
195  
-          return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
196  
-          @#{ivar} = ivar.duplicable? ? ivar.dup : ivar
197  
-        end
198  
-      RUBY
199  
-      unless options[:instance_reader] == false
200  
-        self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
201  
-          def #{ivar}
202  
-            self.class.#{ivar}
203  
-          end
204  
-        RUBY
205  
-      end
206  
-      instance_variable_set(:"@#{ivar}", yield) if block_given?
207  
-    end
208  
-  end
209  
-
210  
-  # Defines class-level inheritable attribute writer. Attributes are available to subclasses,
211  
-  # each subclass has a copy of parent's attribute.
212  
-  #
213  
-  # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
214  
-  #   define inheritable writer for.
215  
-  # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
216  
-  # @return <Array[#to_s]> An Array of the attributes that were made into inheritable writers.
217  
-  #
218  
-  # @api public
219  
-  #
220  
-  # @todo We need a style for class_eval <<-HEREDOC. I'd like to make it
221  
-  #   class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere.
222  
-  def extlib_inheritable_writer(*ivars)
223  
-    options = ivars.extract_options!
224  
-
225  
-    ivars.each do |ivar|
226  
-      self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
227  
-        def self.#{ivar}=(obj)
228  
-          @#{ivar} = obj
229  
-        end
230  
-      RUBY
231  
-      unless options[:instance_writer] == false
232  
-        self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
233  
-          def #{ivar}=(obj) self.class.#{ivar} = obj end
234  
-        RUBY
235  
-      end
236  
-
237  
-      self.send("#{ivar}=", yield) if block_given?
238  
-    end
239  
-  end
240  
-
241  
-  # Defines class-level inheritable attribute accessor. Attributes are available to subclasses,
242  
-  # each subclass has a copy of parent's attribute.
243  
-  #
244  
-  # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
245  
-  #   define inheritable accessor for.
246  
-  # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
247  
-  # @return <Array[#to_s]> An Array of attributes turned into inheritable accessors.
248  
-  #
249  
-  # @api public
250  
-  def extlib_inheritable_accessor(*syms, &block)
251  
-    extlib_inheritable_reader(*syms)
252  
-    extlib_inheritable_writer(*syms, &block)
253  
-  end
254  
-end

0 notes on commit ae44bf7

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