Skip to content

Commit

Permalink
Add more standard Hash methods to ActiveSupport::OrderedHash [#314 st…
Browse files Browse the repository at this point in the history
…ate:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
purcell authored and josh committed Jun 3, 2008
1 parent d54d90f commit 7cfa6ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,9 @@
*Edge*

* Add more standard Hash methods to ActiveSupport::OrderedHash [Steve Purcell]

* Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport [Josh Peek]

* Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger)


Expand Down
14 changes: 14 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
Expand Up @@ -38,6 +38,20 @@ def to_hash
each { |array| hash[array[0]] = array[1] }
end
end

def has_key?(k)
!assoc(k).nil?
end

alias_method :key?, :has_key?
alias_method :include?, :has_key?
alias_method :member?, :has_key?

def has_value?(v)
any? { |key, value| value == v }
end

alias_method :value?, :has_value?
end
end
end
19 changes: 19 additions & 0 deletions activesupport/test/ordered_hash_test.rb
Expand Up @@ -42,4 +42,23 @@ def test_delete

assert_nil @ordered_hash.delete(bad_key)
end

def test_has_key
assert_equal true, @ordered_hash.has_key?('blue')
assert_equal true, @ordered_hash.key?('blue')
assert_equal true, @ordered_hash.include?('blue')
assert_equal true, @ordered_hash.member?('blue')

assert_equal false, @ordered_hash.has_key?('indigo')
assert_equal false, @ordered_hash.key?('indigo')
assert_equal false, @ordered_hash.include?('indigo')
assert_equal false, @ordered_hash.member?('indigo')
end

def test_has_value
assert_equal true, @ordered_hash.has_value?('000099')
assert_equal true, @ordered_hash.value?('000099')
assert_equal false, @ordered_hash.has_value?('ABCABC')
assert_equal false, @ordered_hash.value?('ABCABC')
end
end

0 comments on commit 7cfa6ec

Please sign in to comment.