Permalink
Browse files

Create a seperate file for ActiveSupport::OrderedHash.

  • Loading branch information...
1 parent 7708650 commit 49846f85864f4b59d5593c30ee3c59b584d2975d @josh josh committed May 14, 2008
@@ -39,6 +39,7 @@
require 'active_support/dependencies'
require 'active_support/deprecation'
+require 'active_support/ordered_hash'
require 'active_support/ordered_options'
require 'active_support/option_merger'
@@ -0,0 +1,43 @@
+# OrderedHash is namespaced to prevent conflicts with other implementations
+module ActiveSupport
+ # Hash is ordered in Ruby 1.9!
+ if RUBY_VERSION >= '1.9'
+ OrderedHash = ::Hash
+ else
+ class OrderedHash < Array #:nodoc:
+ def []=(key, value)
+ if pair = assoc(key)
+ pair.pop
+ pair << value
+ else
+ self << [key, value]
+ end
+ end
+
+ def [](key)
+ pair = assoc(key)
+ pair ? pair.last : nil
+ end
+
+ def delete(key)
+ pair = assoc(key)
+ pair ? array_index = index(pair) : nil
+ array_index ? delete_at(array_index).last : nil
+ end
+
+ def keys
+ collect { |key, value| key }
+ end
+
+ def values
+ collect { |key, value| value }
+ end
+
+ def to_hash
+ returning({}) do |hash|
+ each { |array| hash[array[0]] = array[1] }
+ end
+ end
+ end
+ end
+end
@@ -1,47 +1,3 @@
-# OrderedHash is namespaced to prevent conflicts with other implementations
-module ActiveSupport
- # Hash is ordered in Ruby 1.9!
- if RUBY_VERSION >= '1.9'
- OrderedHash = ::Hash
- else
- class OrderedHash < Array #:nodoc:
- def []=(key, value)
- if pair = assoc(key)
- pair.pop
- pair << value
- else
- self << [key, value]
- end
- end
-
- def [](key)
- pair = assoc(key)
- pair ? pair.last : nil
- end
-
- def delete(key)
- pair = assoc(key)
- pair ? array_index = index(pair) : nil
- array_index ? delete_at(array_index).last : nil
- end
-
- def keys
- collect { |key, value| key }
- end
-
- def values
- collect { |key, value| value }
- end
-
- def to_hash
- returning({}) do |hash|
- each { |array| hash[array[0]] = array[1] }
- end
- end
- end
- end
-end
-
class OrderedOptions < ActiveSupport::OrderedHash #:nodoc:
def []=(key, value)
super(key.to_sym, value)
@@ -0,0 +1,45 @@
+require 'abstract_unit'
+
+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.each_with_index do |key, index|
+ @ordered_hash[key] = @values[index]
+ end
+ 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
+
+ def test_delete
+ key, value = 'white', 'ffffff'
+ bad_key = 'black'
+
+ @ordered_hash[key] = value
+ assert_equal @keys.length + 1, @ordered_hash.length
+
+ assert_equal value, @ordered_hash.delete(key)
+ assert_equal @keys.length, @ordered_hash.length
+
+ assert_nil @ordered_hash.delete(bad_key)
+ end
+end
@@ -1,49 +1,5 @@
require 'abstract_unit'
-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.each_with_index do |key, index|
- @ordered_hash[key] = @values[index]
- end
- 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
-
- def test_delete
- key, value = 'white', 'ffffff'
- bad_key = 'black'
-
- @ordered_hash[key] = value
- assert_equal @keys.length + 1, @ordered_hash.length
-
- assert_equal value, @ordered_hash.delete(key)
- assert_equal @keys.length, @ordered_hash.length
-
- assert_nil @ordered_hash.delete(bad_key)
- end
-end
-
class OrderedOptionsTest < Test::Unit::TestCase
def test_usage
a = OrderedOptions.new

0 comments on commit 49846f8

Please sign in to comment.