hash-proxy 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +6 -0
- data/lib/hash_proxy/proxy.rb +11 -11
- data/spec/hash_proxy_spec.rb +6 -0
- data/version.txt +1 -1
- metadata +1 -1
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# == Version 0.1.1 / 2012-06-12
|
2
|
+
# * Work around weird behavior when using [] method
|
3
|
+
# to access keys which are methods defined on Kernel,
|
4
|
+
# like Kernel#format. TODO: inherit from BasicObject to
|
5
|
+
# avoid this?
|
6
|
+
#
|
1
7
|
# == Version 0.1.0 / 2012-06-12
|
2
8
|
# * include Enumerable in Proxy
|
3
9
|
# * Add some hash-like semantics to Proxy
|
data/lib/hash_proxy/proxy.rb
CHANGED
@@ -56,7 +56,14 @@ module HashProxy
|
|
56
56
|
# @param [Object] key The value to look up
|
57
57
|
# @return [Object] The object associated with the key
|
58
58
|
def [](key)
|
59
|
-
|
59
|
+
# Return the value if it has already been converted
|
60
|
+
if @converted.has_key?(key)
|
61
|
+
@converted[key]
|
62
|
+
else
|
63
|
+
# Move the value from the original hash to the converted hash.
|
64
|
+
# Support both symbol or string keys
|
65
|
+
self.move_value(key)
|
66
|
+
end
|
60
67
|
end
|
61
68
|
|
62
69
|
# Sets a value after converting it to a Proxy if necessary
|
@@ -64,7 +71,7 @@ module HashProxy
|
|
64
71
|
# @param [Object] key The key of the value to set
|
65
72
|
# @param [Object] value The value to set
|
66
73
|
def []=(key, value)
|
67
|
-
|
74
|
+
@converted[key] = convert_value(value)
|
68
75
|
end
|
69
76
|
|
70
77
|
# Turns arbitrary method invocations into
|
@@ -74,19 +81,12 @@ module HashProxy
|
|
74
81
|
if name_str.end_with?(EQUALS)
|
75
82
|
# Handle edge case (self.send(:"=", 'foo') ? why would someone do this)
|
76
83
|
if name_str != EQUALS
|
77
|
-
|
84
|
+
self[name_str[0..-2].to_sym] = args.first
|
78
85
|
else
|
79
86
|
super
|
80
87
|
end
|
81
88
|
else
|
82
|
-
|
83
|
-
if @converted.has_key?(name)
|
84
|
-
@converted[name]
|
85
|
-
else
|
86
|
-
# Move the value from the original hash to the converted hash.
|
87
|
-
# Support both symbol or string keys
|
88
|
-
self.move_value(name, name_str)
|
89
|
-
end
|
89
|
+
self[name]
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
data/spec/hash_proxy_spec.rb
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
require File.expand_path('../spec_helper', __FILE__)
|
3
3
|
|
4
4
|
describe HashProxy do
|
5
|
+
it "does not get confused by methods defined on Kernel" do
|
6
|
+
proxy = HashProxy::Proxy.new({})
|
7
|
+
lambda { proxy[:format] }.should_not raise_error
|
8
|
+
proxy[:format].should be_nil
|
9
|
+
end
|
10
|
+
|
5
11
|
it "supports enumerable" do
|
6
12
|
hash = {foo: 'bar', baz: 'bip', smee: 'cree'}
|
7
13
|
proxy = HashProxy::Proxy.new(hash)
|
data/version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|