Sha256: 4d11cec9f4572b5b9cce96dea801ccf44e48514fd84ab13897b8d8eabdd368f8

Contents?: true

Size: 975 Bytes

Versions: 24

Compression:

Stored size: 975 Bytes

Contents

module Hooks
  module InheritableAttribute
    # Creates an inheritable attribute with accessors in the singleton class. Derived classes inherit the
    # attributes. This is especially helpful with arrays or hashes that are extended in the inheritance
    # chain. Note that you have to initialize the inheritable attribute.
    #
    # Example:
    #
    #   class Cat
    #     inheritable_attr :drinks
    #     self.drinks = ["Becks"]
    #
    #   class Garfield < Cat
    #     self.drinks << "Fireman's 4"
    #
    # and then, later
    #
    #   Cat.drinks      #=> ["Becks"]
    #   Garfield.drinks #=> ["Becks", "Fireman's 4"]
    def inheritable_attr(name)
      instance_eval %Q{
        def #{name}=(v)
          @#{name} = v
        end
        
        def #{name}
          return @#{name} unless superclass.respond_to?(:#{name}) and value = superclass.#{name}
          @#{name} ||= value.clone # only do this once.
        end
      }
    end
  end  
end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
hooks-0.3.3 lib/hooks/inheritable_attribute.rb
hooks-0.3.2 lib/hooks/inheritable_attribute.rb
hooks-0.3.1 lib/hooks/inheritable_attribute.rb
hooks-0.3.0 lib/hooks/inheritable_attribute.rb
hooks-0.2.2 lib/hooks/inheritable_attribute.rb
hooks-0.2.1 lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.rc.4 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.rc.3 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.rc.2 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.rc.1 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.beta.3 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.beta.2 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.beta.1 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.alpha.9 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.alpha.8 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.0.alpha.7 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-3.0.0.alpha.6 lib/middleman/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-3.0.0.alpha.5 lib/middleman/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-3.0.0.alpha.4 lib/middleman/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb