aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/support/class_attribute_accessors.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/support/class_attribute_accessors.rb')
-rw-r--r--activerecord/lib/active_record/support/class_attribute_accessors.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/support/class_attribute_accessors.rb b/activerecord/lib/active_record/support/class_attribute_accessors.rb
new file mode 100644
index 0000000000..0e269165a6
--- /dev/null
+++ b/activerecord/lib/active_record/support/class_attribute_accessors.rb
@@ -0,0 +1,43 @@
+# attr_* style accessors for class-variables that can accessed both on an instance and class level.
+class Class #:nodoc:
+ def cattr_reader(*syms)
+ syms.each do |sym|
+ class_eval <<-EOS
+ if ! defined? @@#{sym.id2name}
+ @@#{sym.id2name} = nil
+ end
+
+ def self.#{sym.id2name}
+ @@#{sym}
+ end
+
+ def #{sym.id2name}
+ self.class.#{sym.id2name}
+ end
+ EOS
+ end
+ end
+
+ def cattr_writer(*syms)
+ syms.each do |sym|
+ class_eval <<-EOS
+ if ! defined? @@#{sym.id2name}
+ @@#{sym.id2name} = nil
+ end
+
+ def self.#{sym.id2name}=(obj)
+ @@#{sym.id2name} = obj
+ end
+
+ def #{sym.id2name}=(obj)
+ self.class.#{sym.id2name}=(obj)
+ end
+ EOS
+ end
+ end
+
+ def cattr_accessor(*syms)
+ cattr_reader(*syms)
+ cattr_writer(*syms)
+ end
+end \ No newline at end of file