public
Rubygem
Description: ObjectProxy provides a proxied interface to Ruby objects. It lets you add methods to objects that don't normally support them.
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/object_proxy.git
object_proxy / lib / object_proxy_safe_hash.rb
100644 31 lines (25 sloc) 0.767 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# This extension fixes that object proxies aren't interchangeable with their target
# value for the purposes of hash lookup
# The following becomes true in MRI:
#
# {'some_string' => true}[ObjectProxy.new('some_string')]
#
 
module ObjectProxySafeHash
  def self.included(klass)
    klass.class_eval do
 
      define_method "[]_with_object_proxy" do |value|
        if value.respond_to?(:is_object_proxy?) && value.is_object_proxy?
          value = value.target
        end
        send "[]_without_object_proxy", value
      end
      
      alias_method '[]_without_object_proxy', '[]'
      alias_method '[]', '[]_with_object_proxy'
    end
 
  end
end
 
class Hash
  unless included_modules.include?(ObjectProxySafeHash)
    include ObjectProxySafeHash
  end
end