Sha256: c79623e7ff9b24e671ca5d4d33517478457808ee4ed1fcb28d682689dcecd817

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

# Allows attributes to be shared within an inheritance hierarchy, but where each descendant gets a copy of
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
# to, for example, an array without those additions being shared with either their parent, siblings, or
# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
module ClassInheritableAttributes # :nodoc:
  def self.append_features(base)
    super
    base.extend(ClassMethods)
  end
  
  module ClassMethods # :nodoc:
    @@classes ||= {}
    
    def inheritable_attributes
      @@classes[self] ||= {}
    end
    
    def write_inheritable_attribute(key, value)
      inheritable_attributes[key] = value
    end
    
    def write_inheritable_array(key, elements)
      write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil?
      write_inheritable_attribute(key, read_inheritable_attribute(key) + elements)
    end

    def read_inheritable_attribute(key)
      inheritable_attributes[key]
    end
    
    def reset_inheritable_attributes
      inheritable_attributes.clear
    end

    private 
      def inherited(child)
        @@classes[child] = inheritable_attributes.dup
      end
   
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
actionpack-1.0.1 lib/action_controller/support/class_inheritable_attributes.rb
actionpack-1.0.0 lib/action_controller/support/class_inheritable_attributes.rb
actionpack-1.1.0 lib/action_controller/support/class_inheritable_attributes.rb
actionpack-1.2.0 lib/action_controller/support/class_inheritable_attributes.rb
activerecord-1.4.0 lib/active_record/support/class_inheritable_attributes.rb