Sha256: 39f2eb976b1dc86540b69810cc3d3ca7ce6d946a29815670c77231843363c1af

Contents?: true

Size: 965 Bytes

Versions: 52

Compression:

Stored size: 965 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

52 entries across 52 versions & 2 rubygems

Version Path
middleman-core-3.2.2 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.2.1 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.2.0 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.6 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.5 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.4 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.3 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.2 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.1 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.rc.4 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.rc.3 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.rc.2 lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.rc.1 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-x86-mingw32-3.0.14 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.0.14 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.beta.3 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.beta.2 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-3.1.0.beta.1 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
middleman-core-x86-mingw32-3.0.13 lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb