require 'active_support'

class Class
def class_inheritable_attributes(*syms, &block)
eigenclass = class << self; self; end
syms.each do |sym|
eigenclass.send :define_method, sym do |*values|
if value = values.first
value = block.call value if block
write_inheritable_attribute sym, value
else
read_inheritable_attribute sym
end
end
class_eval <<-EOS
def #{sym}
self.class.read_inheritable_attribute :#{sym}
end
EOS
end
end

alias :class_inheritable_attribute :class_inheritable_attributes
end

## examples

# class Base
# class_inheritable_attribute(:root) { |root| Pathname.new(root).realpath }
# end

# class A < Base
# root '.'
# end

# >> Base.root
# => nil
# >> a = A.new
# => #<A:0xb78a2c64>
# >> a.root
# => #<Pathname:/home/vjt/code/ecole/lib>
# >> A.root
# => #<Pathname:/home/vjt/code/ecole/lib>
# >> A.root '.'
# => #<Pathname:/home/vjt/code/ecole/lib>
# >> A.root '..'
# => #<Pathname:/home/vjt/code/ecole>

# vjt@openssl.it