Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/uber/inheritable_attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ module Uber
module InheritableAttr

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
name = name.to_s
variable = "@#{name}"
instance_variable_defined?(variable) # validate the name
method = name.to_sym
options = options.dup

define_singleton_method(:"#{name}=") do |value|
instance_variable_set(variable, value)
end

define_singleton_method(method) do
return instance_variable_get(variable) if instance_variable_defined?(variable)
instance_variable_set(variable, InheritableAttribute.inherit_for(self, method, options))
end
end

def self.inherit_for(klass, name, options={})
Expand Down