Module: ObjectProxySafeHash

Included in:
Hash
Defined in:
lib/object_proxy_safe_hash.rb

Overview

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

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/object_proxy_safe_hash.rb', line 9

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