Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v3.0.0.beta1
Fetching contributors…

Cannot retrieve contributors at this time

37 lines (35 sloc) 1.069 kb
require 'active_support/core_ext/object/metaclass'
require 'active_support/core_ext/module/delegation'
class Class
# Declare a class-level attribute whose value is inheritable and
# overwritable by subclasses:
#
# class Base
# class_attribute :setting
# end
#
# class Subclass < Base
# end
#
# Base.setting = true
# Subclass.setting # => true
# Subclass.setting = false
# Subclass.setting # => false
# Base.setting # => true
#
# This matches normal Ruby method inheritance: think of writing an attribute
# on a subclass as overriding the reader method.
#
# For convenience, a query method is defined as well:
#
# Subclass.setting? # => false
def class_attribute(*attrs)
attrs.each do |attr|
metaclass.send(:define_method, attr) { }
metaclass.send(:define_method, "#{attr}?") { !!send(attr) }
metaclass.send(:define_method, "#{attr}=") do |value|
metaclass.send(:define_method, attr) { value }
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.