aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-09-11 08:23:50 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-09-11 08:23:50 +0000
commit8ae68efcf84d9c9927c4db7dfeb0f41693b9e555 (patch)
tree777b582e86de7194091067155d0e17aca48d395f /activesupport/lib/active_support/core_ext
parentc1007377ba010448e55030c37f3fee24208e9912 (diff)
downloadrails-8ae68efcf84d9c9927c4db7dfeb0f41693b9e555.tar.gz
rails-8ae68efcf84d9c9927c4db7dfeb0f41693b9e555.tar.bz2
rails-8ae68efcf84d9c9927c4db7dfeb0f41693b9e555.zip
Added Hash#reverse_merge, Hash#reverse_merge!, and Hash#reverse_update to ease the use of default options. Added :connector and :skip_last_comma options to Array#to_sentence
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb20
-rw-r--r--activesupport/lib/active_support/core_ext/hash.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/reverse_merge.rb25
3 files changed, 41 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
index 19220da2b1..e9e9cf445e 100644
--- a/activesupport/lib/active_support/core_ext/array/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -3,12 +3,20 @@ module ActiveSupport #:nodoc:
module Array #:nodoc:
# Enables to conversion of Arrays to human readable lists. ['one', 'two', 'three'] => "one, two, and three"
module Conversions
- # Converts the array to comma-seperated sentence where the last element is joined by the connector word (default is 'and').
- def to_sentence(connector = 'and')
- if length > 1
- "#{self[0...-1].join(', ')}, #{connector} #{self[-1]}"
- elsif length == 1
- self[0]
+ # Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options:
+ # * <tt>:connector</tt>: The word used to join the last element in arrays with more than two elements (default: "and")
+ # * <tt>:skip_last_comma</tt>: Set to true to return "a, b and c" instead of "a, b, and c".
+ def to_sentence(options = {})
+ options.assert_valid_keys(:connector, :skip_last_comma)
+ options.reverse_merge! :connector => 'and', :skip_last_comma => false
+
+ case length
+ when 1
+ self[0]
+ when 2
+ "#{self[0]} #{options[:connector]} #{self[1]}"
+ else
+ "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]} #{self[-1]}"
end
end
diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb
index e899d3e1e2..f16368b18b 100644
--- a/activesupport/lib/active_support/core_ext/hash.rb
+++ b/activesupport/lib/active_support/core_ext/hash.rb
@@ -1,7 +1,9 @@
require File.dirname(__FILE__) + '/hash/keys'
require File.dirname(__FILE__) + '/hash/indifferent_access'
+require File.dirname(__FILE__) + '/hash/reverse_merge'
class Hash #:nodoc:
include ActiveSupport::CoreExtensions::Hash::Keys
include ActiveSupport::CoreExtensions::Hash::IndifferentAccess
+ include ActiveSupport::CoreExtensions::Hash::ReverseMerge
end
diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
new file mode 100644
index 0000000000..46c53871d8
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
@@ -0,0 +1,25 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Hash #:nodoc:
+ # Allows for reverse merging where its the keys in the calling hash that wins over those in the <tt>other_hash</tt>.
+ # This is particularly useful for initializing an incoming option hash with default values:
+ #
+ # def setup(options = {})
+ # options.reverse_merge! :size => 25, :velocity => 10
+ # end
+ #
+ # The default :size and :velocity is only set if the +options+ passed in doesn't already have those keys set.
+ module ReverseMerge
+ def reverse_merge(other_hash)
+ other_hash.merge(self)
+ end
+
+ def reverse_merge!(other_hash)
+ replace(reverse_merge(other_hash))
+ end
+
+ alias_method :reverse_update, :reverse_merge
+ end
+ end
+ end
+end \ No newline at end of file