aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/class/attribute.rb
diff options
context:
space:
mode:
authorJames Miller <bensie@gmail.com>2011-06-18 15:49:11 -0700
committerJames Miller <bensie@gmail.com>2011-06-18 15:49:11 -0700
commit0ce5fb8a2743042d05f8f43bf2ea6fb7924a2ce2 (patch)
tree3f076a9824dbc356cb1bb92d93889beff14b3c2f /activesupport/lib/active_support/core_ext/class/attribute.rb
parent2fbb7504e2c2b0a95398d1ef0c97ea4a403d831d (diff)
downloadrails-0ce5fb8a2743042d05f8f43bf2ea6fb7924a2ce2.tar.gz
rails-0ce5fb8a2743042d05f8f43bf2ea6fb7924a2ce2.tar.bz2
rails-0ce5fb8a2743042d05f8f43bf2ea6fb7924a2ce2.zip
Add option to omit creating an instance reader method on class_attribute
Diffstat (limited to 'activesupport/lib/active_support/core_ext/class/attribute.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute.rb27
1 files changed, 20 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb
index 7baba75ad3..0d4033329e 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute.rb
@@ -56,11 +56,22 @@ class Class
# object.setting # => false
# Base.setting # => true
#
+ # To opt out of the instance reader method, pass :instance_reader => false.
+ #
+ # object.setting # => NoMethodError
+ # object.setting? # => NoMethodError
+ #
# To opt out of the instance writer method, pass :instance_writer => false.
#
# object.setting = false # => NoMethodError
def class_attribute(*attrs)
- instance_writer = !attrs.last.is_a?(Hash) || attrs.pop[:instance_writer]
+ instance_reader = true
+ instance_writer = true
+ if attrs.last.is_a?(Hash)
+ instance_accessors = attrs.pop
+ instance_reader = instance_accessors[:instance_reader]
+ instance_writer = instance_accessors[:instance_writer]
+ end
attrs.each do |name|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -84,13 +95,15 @@ class Class
val
end
- remove_possible_method :#{name}
- def #{name}
- defined?(@#{name}) ? @#{name} : self.class.#{name}
- end
+ if instance_reader
+ remove_possible_method :#{name}
+ def #{name}
+ defined?(@#{name}) ? @#{name} : self.class.#{name}
+ end
- def #{name}?
- !!#{name}
+ def #{name}?
+ !!#{name}
+ end
end
RUBY