aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2006-06-02 17:28:15 +0000
committerSam Stephenson <sam@37signals.com>2006-06-02 17:28:15 +0000
commitc377ce50ed871a90a7e4f0d4d69062d01934ce85 (patch)
tree51ebf9b11009216659a92c87f9aaa64a21cbdbbe
parent8cdf9126d3c3174cad74f8ae4a913f1940d3474a (diff)
downloadrails-c377ce50ed871a90a7e4f0d4d69062d01934ce85.tar.gz
rails-c377ce50ed871a90a7e4f0d4d69062d01934ce85.tar.bz2
rails-c377ce50ed871a90a7e4f0d4d69062d01934ce85.zip
Add OrderedHash#values
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4411 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/ordered_options.rb6
-rw-r--r--activesupport/test/ordered_options_test.rb27
3 files changed, 34 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 65eff81083..c6fb0f82b5 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add OrderedHash#values. [Sam Stephenson]
+
* Added Array#to_s(:db) that'll produce a comma-separated list of ids [DHH]. Example:
Purchase.find(:all, :conditions => "product_id IN (#{shops.products.to_s(:db)})"
diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb
index 48b5c92454..0704549ce0 100644
--- a/activesupport/lib/active_support/ordered_options.rb
+++ b/activesupport/lib/active_support/ordered_options.rb
@@ -16,7 +16,11 @@ module ActiveSupport
end
def keys
- self.collect { |i| i.first }
+ collect { |key, value| key }
+ end
+
+ def values
+ collect { |key, value| value }
end
private
diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb
index 0247331c6a..05bd5ef932 100644
--- a/activesupport/test/ordered_options_test.rb
+++ b/activesupport/test/ordered_options_test.rb
@@ -2,6 +2,33 @@ require 'test/unit'
require File.dirname(__FILE__) + '/../lib/active_support/ordered_options'
+class OrderedHashTest < Test::Unit::TestCase
+ def setup
+ @keys = %w( blue green red pink orange )
+ @values = %w( 000099 009900 aa0000 cc0066 cc6633 )
+ @ordered_hash = ActiveSupport::OrderedHash.new(@keys.zip(@values))
+ end
+
+ def test_order
+ assert_equal @keys, @ordered_hash.keys
+ assert_equal @values, @ordered_hash.values
+ end
+
+ def test_access
+ assert @keys.zip(@values).all? { |k, v| @ordered_hash[k] == v }
+ end
+
+ def test_assignment
+ key, value = 'purple', '5422a8'
+
+ @ordered_hash[key] = value
+ assert_equal @keys.length + 1, @ordered_hash.length
+ assert_equal key, @ordered_hash.keys.last
+ assert_equal value, @ordered_hash.values.last
+ assert_equal value, @ordered_hash[key]
+ end
+end
+
class OrderedOptionsTest < Test::Unit::TestCase
def test_usage
a = OrderedOptions.new