Module: Uber::InheritableAttr

Defined in:
lib/uber/inheritable_attr.rb

Defined Under Namespace

Classes: Clone

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherit_for(klass, name, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/uber/inheritable_attr.rb', line 17

def self.inherit_for(klass, name, options={})
  return unless klass.superclass.respond_to?(name)

  value = klass.superclass.send(name) # could be nil

  return value if options[:clone] == false
  Clone.(value) # this could be dynamic, allowing other inheritance strategies.
end

Instance Method Details

#inheritable_attr(name, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/uber/inheritable_attr.rb', line 4

def inheritable_attr(name, options={})
  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}=(v)
      @#{name} = v
    end

    def #{name}
      return @#{name} if instance_variable_defined?(:@#{name})
      @#{name} = InheritableAttribute.inherit_for(self, :#{name}, #{options})
    end
  RUBY
end