aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/array/extract_options.rb
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-03-27 02:59:10 -0700
committerwycats <wycats@gmail.com>2010-03-27 02:59:10 -0700
commita24a888afe5652e391ce2ea682596686af5ec58b (patch)
treef1a4c43fe0faecc64084a90fcec530615c91c66d /activesupport/lib/active_support/core_ext/array/extract_options.rb
parentb081948bb3e1a6874f133140bf07e7fb9d3359d9 (diff)
downloadrails-a24a888afe5652e391ce2ea682596686af5ec58b.tar.gz
rails-a24a888afe5652e391ce2ea682596686af5ec58b.tar.bz2
rails-a24a888afe5652e391ce2ea682596686af5ec58b.zip
Limit Array#extract_options! to directl instances of Hash and HWIA. Add extractable_options? to Hash so that subclasses of Hash can opt-into extractable behavior. This fixes an issue where respond_with wasn't working with subclasses of Hash that were provided by other libraries (such as CouchDB or Mashie) [#4145 state:resolved]
Diffstat (limited to 'activesupport/lib/active_support/core_ext/array/extract_options.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/array/extract_options.rb17
1 files changed, 16 insertions, 1 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