Report abuse


			
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.root
# => #
# >> A.root
# => #
# >> A.root '.'
# => #
# >> A.root '..'
# => #

# vjt@openssl.it