Skip to content

Commit

Permalink
Create a seperate file for ActiveSupport::OrderedHash.
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 14, 2008
1 parent 7708650 commit 49846f8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 88 deletions.
1 change: 1 addition & 0 deletions activesupport/lib/active_support.rb
Expand Up @@ -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'

Expand Down
43 changes: 43 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
@@ -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
44 changes: 0 additions & 44 deletions activesupport/lib/active_support/ordered_options.rb
@@ -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)
Expand Down
45 changes: 45 additions & 0 deletions activesupport/test/ordered_hash_test.rb
@@ -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
44 changes: 0 additions & 44 deletions activesupport/test/ordered_options_test.rb
@@ -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
Expand Down

0 comments on commit 49846f8

Please sign in to comment.