aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-04-10 16:16:31 -0700
committerwycats <wycats@gmail.com>2010-04-10 16:16:31 -0700
commit76e0a9eb5b5ad17d51dad5e4e8c5ea1ed504ea88 (patch)
treebaf26b3672f7512a892524aeab067c0d50c48cad /activesupport
parent093ab3ec6e476f53a84eb90a3b1944c7e961f204 (diff)
downloadrails-76e0a9eb5b5ad17d51dad5e4e8c5ea1ed504ea88.tar.gz
rails-76e0a9eb5b5ad17d51dad5e4e8c5ea1ed504ea88.tar.bz2
rails-76e0a9eb5b5ad17d51dad5e4e8c5ea1ed504ea88.zip
Not using class_eval wasn't adding clarity here
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute.rb39
1 files changed, 24 insertions, 15 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb
index 725acad43a..d2bcd7a778 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute.rb
@@ -1,5 +1,4 @@
require 'active_support/core_ext/kernel/singleton_class'
-require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/remove_method'
class Class
@@ -41,21 +40,31 @@ class Class
def class_attribute(*attrs)
instance_writer = !attrs.last.is_a?(Hash) || attrs.pop[:instance_writer]
- s = singleton_class
- attrs.each do |attr|
- s.send(:define_method, attr) { }
- s.send(:define_method, :"#{attr}?") { !!send(attr) }
- s.send(:define_method, :"#{attr}=") do |value|
- singleton_class.remove_possible_method(attr)
- singleton_class.send(:define_method, attr) { value }
- end
+ attrs.each do |name|
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def self.#{name}() nil end
+ def self.#{name}?() !!#{name} end
+
+ def self.#{name}=(val)
+ singleton_class.class_eval do
+ remove_possible_method(:#{name})
+ define_method(:#{name}) { val }
+ end
+ end
+
+ def #{name}
+ defined?(@#{name}) ? @#{name} : singleton_class.#{name}
+ end
- define_method(attr) { self.singleton_class.send(attr) }
- define_method(:"#{attr}?") { !!send(attr) }
- define_method(:"#{attr}=") do |value|
- singleton_class.remove_possible_method(attr)
- singleton_class.send(:define_method, attr) { value }
- end if instance_writer
+ def #{name}?
+ !!#{name}
+ end
+ RUBY
+
+ if instance_writer
+ body = "def #{name}=(value) @#{name} = value end"
+ class_eval body, __FILE__, __LINE__ - 1
+ end
end
end
end