class Class # :nodoc: def cattr_reader(*syms) syms.flatten.each do |sym| class_eval(<<-EOS, __FILE__, __LINE__) unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym} @@#{sym} end def #{sym} @@#{sym} end EOS end end def cattr_writer(*syms) syms.flatten.each do |sym| class_eval(<<-EOS, __FILE__, __LINE__) unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym}=(obj) @@#{sym} = obj end def #{sym}=(obj) @@#{sym} = obj end EOS end end def cattr_accessor(*syms) cattr_reader(*syms) cattr_writer(*syms) end def meta_reader(*syms) syms.each do |sym| class_eval <<-EOS def self.#{sym} read_meta_attribute(:#{sym}) end def #{sym} self.class.#{sym} end EOS end end def meta_writer(*syms) syms.each do |sym| class_eval <<-EOS def self.#{sym}=(obj) write_meta_attribute(:#{sym}, obj) end def #{sym}=(obj) self.class.#{sym} = obj end EOS end end def meta_accessor(*syms) meta_reader(*syms) meta_writer(*syms) end def meta_attributes @meta_attributes ||= {} end def write_meta_attribute(key, value) meta_attributes[key] = value end def read_meta_attribute(key) meta_attributes[key] end def reset_meta_attributes meta_attributes.clear end private def inherited_with_meta_attributes(child) inherited_without_meta_attributes(child) if respond_to?(:inherited_without_meta_attributes) new_meta_attributes = meta_attributes.inject({}) do |memo, (key, value)| memo.update(key => (value.dup rescue value)) end child.instance_variable_set('@meta_attributes', new_meta_attributes) end alias inherited_without_meta_attributes inherited alias inherited inherited_with_meta_attributes end