1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Create a seperate file for ActiveSupport::OrderedHash.

This commit is contained in:
Joshua Peek 2008-05-14 12:48:37 -05:00
parent 7708650f73
commit 49846f8586
5 changed files with 89 additions and 88 deletions

View file

@ -39,6 +39,7 @@ require 'active_support/cache'
require 'active_support/dependencies'
require 'active_support/deprecation'
require 'active_support/ordered_hash'
require 'active_support/ordered_options'
require 'active_support/option_merger'

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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