aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/array/extract_options.rb17
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute_accessors.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/module/attribute_accessors.rb14
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb4
4 files changed, 37 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/extract_options.rb b/activesupport/lib/active_support/core_ext/array/extract_options.rb
index 9ca32dc7aa..40ceb3eb9e 100644
--- a/activesupport/lib/active_support/core_ext/array/extract_options.rb
+++ b/activesupport/lib/active_support/core_ext/array/extract_options.rb
@@ -1,3 +1,14 @@
+class Hash
+ # By default, only instances of Hash itself are extractable.
+ # Subclasses of Hash may implement this method and return
+ # true to declare themselves as extractable. If a Hash
+ # is extractable, Array#extract_options! pops it from
+ # the Array when it is the last element of the Array.
+ def extractable_options?
+ instance_of?(Hash)
+ end
+end
+
class Array
# Extracts options from a set of arguments. Removes and returns the last
# element in the array if it's a hash, otherwise returns a blank hash.
@@ -9,6 +20,10 @@ class Array
# options(1, 2) # => {}
# options(1, 2, :a => :b) # => {:a=>:b}
def extract_options!
- last.is_a?(::Hash) ? pop : {}
+ if last.is_a?(Hash) && last.extractable_options?
+ pop
+ else
+ {}
+ end
end
end
diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
index 1602a609eb..83d154be5c 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -10,8 +10,8 @@ require 'active_support/core_ext/array/extract_options'
# Person.hair_colors = [:brown, :black, :blonde, :red]
class Class
def cattr_reader(*syms)
+ options = syms.extract_options!
syms.flatten.each do |sym|
- next if sym.is_a?(Hash)
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@hair_colors
@@#{sym} = nil # @@hair_colors = nil
@@ -20,11 +20,14 @@ class Class
def self.#{sym} # def self.hair_colors
@@#{sym} # @@hair_colors
end # end
- #
- def #{sym} # def hair_colors
- @@#{sym} # @@hair_colors
- end # end
EOS
+ unless options[:instance_reader] == false
+ class_eval(<<-EOS, __FILE__, __LINE__)
+ def #{sym} # def hair_colors
+ @@#{sym} # @@hair_colors
+ end # end
+ EOS
+ end
end
end
diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
index 131b512944..c315fd5812 100644
--- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
@@ -2,7 +2,7 @@ require 'active_support/core_ext/array/extract_options'
class Module
def mattr_reader(*syms)
- syms.extract_options!
+ options = syms.extract_options!
syms.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@pagination_options
@@ -12,11 +12,15 @@ class Module
def self.#{sym} # def self.pagination_options
@@#{sym} # @@pagination_options
end # end
-
- def #{sym} # def pagination_options
- @@#{sym} # @@pagination_options
- end # end
EOS
+
+ unless options[:instance_reader] == false
+ class_eval(<<-EOS, __FILE__, __LINE__)
+ def #{sym} # def hair_colors
+ @@#{sym} # @@hair_colors
+ end # end
+ EOS
+ end
end
end
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 543dab4a75..8241b69c8b 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -4,6 +4,10 @@
module ActiveSupport
class HashWithIndifferentAccess < Hash
+ def extractable_options?
+ true
+ end
+
def initialize(constructor = {})
if constructor.is_a?(Hash)
super()