Module: Hooks::InheritableAttribute

Defined in:
lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb

Instance Method Summary collapse

Instance Method Details

#inheritable_attr(name) ⇒ Object

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"]


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks/inheritable_attribute.rb', line 20

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