Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 3148216cbb2fb38c9c83ab91dde96ea25d46dc7c 1 parent a821c3d
@jeremy jeremy authored
View
47 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)
View
37 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 @@ def mattr_accessor(*syms)
mattr_reader(*syms)
mattr_writer(*syms)
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.