From 3148216cbb2fb38c9c83ab91dde96ea25d46dc7c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 21 Nov 2005 07:29:12 +0000 Subject: r3216@asus: jeremy | 2005-11-20 03:05:20 -0800 simplify cattr_ and mattr_accessor git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3128 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/class_attribute_accessors.rb | 47 ++++++++-------------- .../active_support/module_attribute_accessors.rb | 37 ++++++----------- 2 files changed, 29 insertions(+), 55 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/class_attribute_accessors.rb b/activesupport/lib/active_support/class_attribute_accessors.rb index bb043f8e98..93a7d48f69 100644 --- a/activesupport/lib/active_support/class_attribute_accessors.rb +++ b/activesupport/lib/active_support/class_attribute_accessors.rb @@ -1,55 +1,42 @@ -# Extends the class object with class and instance accessors for class attributes, +# Extends the class object with class and instance accessors for class attributes, # just like the native attr* accessors for instance attributes. class Class # :nodoc: def cattr_reader(*syms) - syms.select { |sym| sym.respond_to?(:id2name) }.each do |sym| - class_eval <<-EOS - if ! defined? @@#{sym.id2name} - @@#{sym.id2name} = nil - end - - def self.#{sym.id2name} - @@#{sym} + syms.flatten.each do |sym| + class_eval(<<-EOS, __FILE__, __LINE__) + unless defined? @@#{sym} + @@#{sym} = nil end - def #{sym.id2name} + def self.#{sym} @@#{sym} end - def call_#{sym.id2name} - case @@#{sym.id2name} - when Symbol then send(@@#{sym}) - when Proc then @@#{sym}.call(self) - when String then @@#{sym} - else nil - end + def #{sym} + @@#{sym} end EOS end end - + def cattr_writer(*syms) - syms.select { |sym| sym.respond_to?(:id2name) }.each do |sym| - class_eval <<-EOS - if ! defined? @@#{sym.id2name} - @@#{sym.id2name} = nil - end - - def self.#{sym.id2name}=(obj) - @@#{sym.id2name} = obj + syms.flatten.each do |sym| + class_eval(<<-EOS, __FILE__, __LINE__) + unless defined? @@#{sym} + @@#{sym} = nil end - def self.set_#{sym.id2name}(obj) - @@#{sym.id2name} = obj + def self.#{sym}=(obj) + @@#{sym} = obj end - def #{sym.id2name}=(obj) + def #{sym}=(obj) @@#{sym} = obj end EOS end end - + def cattr_accessor(*syms) cattr_reader(*syms) cattr_writer(*syms) diff --git a/activesupport/lib/active_support/module_attribute_accessors.rb b/activesupport/lib/active_support/module_attribute_accessors.rb index 3d466e4493..fe4f8a4fc0 100644 --- a/activesupport/lib/active_support/module_attribute_accessors.rb +++ b/activesupport/lib/active_support/module_attribute_accessors.rb @@ -3,47 +3,34 @@ class Module # :nodoc: def mattr_reader(*syms) syms.each do |sym| - class_eval <<-EOS - if ! defined? @@#{sym.id2name} - @@#{sym.id2name} = nil + class_eval(<<-EOS, __FILE__, __LINE__) + unless defined? @@#{sym} + @@#{sym} = nil end - def self.#{sym.id2name} + def self.#{sym} @@#{sym} end - def #{sym.id2name} + def #{sym} @@#{sym} end - - def call_#{sym.id2name} - case @@#{sym.id2name} - when Symbol then send(@@#{sym}) - when Proc then @@#{sym}.call(self) - when String then @@#{sym} - else nil - end - end EOS end end def mattr_writer(*syms) syms.each do |sym| - class_eval <<-EOS - if ! defined? @@#{sym.id2name} - @@#{sym.id2name} = nil + class_eval(<<-EOS, __FILE__, __LINE__) + unless defined? @@#{sym} + @@#{sym} = nil end - def self.#{sym.id2name}=(obj) - @@#{sym.id2name} = obj - end - - def self.set_#{sym.id2name}(obj) - @@#{sym.id2name} = obj + def self.#{sym}=(obj) + @@#{sym} = obj end - def #{sym.id2name}=(obj) + def #{sym}=(obj) @@#{sym} = obj end EOS @@ -54,4 +41,4 @@ class Module # :nodoc: mattr_reader(*syms) mattr_writer(*syms) end -end \ No newline at end of file +end -- cgit v1.2.3