class Class

Public Instance Methods

my_class_attribute(*attrs) click to toggle source

Declare a class-level attribute whose value is inheritable by subclasses. Subclasses can change their own value and it will not impact parent class.

Examples

class Base
  my_class_attribute :setting
end

class Subclass < Base
end

Base.setting = true
Subclass.setting            # => true
Subclass.setting = false
Subclass.setting            # => false
Base.setting                # => true

In the above case as long as Subclass does not assign a value to setting by performing Subclass.setting = something, Subclass.setting would read value assigned to parent class. Once Subclass assigns a value then the value assigned by Subclass would be returned.

This matches normal Ruby method inheritance: think of writing an attribute on a subclass as overriding the reader method. However, you need to be aware when using class_attribute with mutable structures as Array or Hash. In such cases, you don't want to do changes in place. Instead use setters:

Base.setting = []
Base.setting                # => []
Subclass.setting            # => []

# Appending in child changes both parent and child because it is the same object:
Subclass.setting << :foo
Base.setting               # => [:foo]
Subclass.setting           # => [:foo]

# Use setters to not propagate changes:
Base.setting = []
Subclass.setting += [:foo]
Base.setting               # => []
Subclass.setting           # => [:foo]

For convenience, an instance predicate method is defined as well. To skip it, pass instance_predicate: false.

Subclass.setting?       # => false

Instances may overwrite the class value in the same way:

Base.setting = true
object = Base.new
object.setting          # => true
object.setting = false
object.setting          # => false
Base.setting            # => true
    # File lib/dbus/core_ext/class/attribute.rb
 64 def my_class_attribute(*attrs)
 65   instance_reader    = true
 66   instance_writer    = true
 67 
 68   attrs.each do |name|
 69     singleton_class.silence_redefinition_of_method(name)
 70     define_singleton_method(name) { nil }
 71 
 72     ivar = "@#{name}".to_sym
 73 
 74     singleton_class.silence_redefinition_of_method("#{name}=")
 75     define_singleton_method("#{name}=") do |val|
 76       singleton_class.class_eval do
 77         redefine_method(name) { val }
 78       end
 79 
 80       if singleton_class?
 81         class_eval do
 82           redefine_method(name) do
 83             if instance_variable_defined? ivar
 84               instance_variable_get ivar
 85             else
 86               singleton_class.send name
 87             end
 88           end
 89         end
 90       end
 91       val
 92     end
 93 
 94     if instance_reader
 95       redefine_method(name) do
 96         if instance_variable_defined?(ivar)
 97           instance_variable_get ivar
 98         else
 99           self.class.public_send name
100         end
101       end
102     end
103 
104     if instance_writer
105       redefine_method("#{name}=") do |val|
106         instance_variable_set ivar, val
107       end
108     end
109   end
110 end