Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a seperate file for ActiveSupport::OrderedHash.
- Loading branch information
Showing
5 changed files
with
89 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters