aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/configurable.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/configurable.rb')
-rw-r--r--activesupport/lib/active_support/configurable.rb26
1 files changed, 19 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb
index 307ae40398..16d2a6a290 100644
--- a/activesupport/lib/active_support/configurable.rb
+++ b/activesupport/lib/active_support/configurable.rb
@@ -13,7 +13,7 @@ module ActiveSupport
self.class.compile_methods!(keys)
end
- # compiles reader methods so we don't have to go through method_missing
+ # Compiles reader methods so we don't have to go through method_missing.
def self.compile_methods!(keys)
keys.reject { |m| method_defined?(m) }.each do |key|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -39,7 +39,7 @@ module ActiveSupport
# Allows you to add shortcut so that you don't have to refer to attribute
# through config. Also look at the example for config to contrast.
- #
+ #
# Defines both class and instance config accessors.
#
# class User
@@ -47,16 +47,16 @@ module ActiveSupport
# config_accessor :allowed_access
# end
#
- # User.allowed_access # => nil
+ # User.allowed_access # => nil
# User.allowed_access = false
- # User.allowed_access # => false
- #
+ # User.allowed_access # => false
+ #
# user = User.new
# user.allowed_access # => false
# user.allowed_access = true
# user.allowed_access # => true
#
- # User.allowed_access # => false
+ # User.allowed_access # => false
#
# The attribute name must be a valid method name in Ruby.
#
@@ -91,7 +91,18 @@ module ActiveSupport
#  User.allowed_access # => false
#
# User.new.allowed_access = true # => NoMethodError
- # User.new.allowed_access # => NoMethodError
+ # User.new.allowed_access # => NoMethodError
+ #
+ # Also you can pass a block to set up the attribute with a default value.
+ #
+ # class User
+ # include ActiveSupport::Configurable
+ # config_accessor :hair_colors do
+ # [:brown, :black, :blonde, :red]
+ # end
+ # end
+ #
+ # User.hair_colors # => [:brown, :black, :blonde, :red]
def config_accessor(*names)
options = names.extract_options!
@@ -108,6 +119,7 @@ module ActiveSupport
class_eval reader, __FILE__, reader_line unless options[:instance_reader] == false
class_eval writer, __FILE__, writer_line unless options[:instance_writer] == false
end
+ send("#{name}=", yield) if block_given?
end
end
end