tainted_hash 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tainted_hash.rb +24 -15
- data/lib/tainted_hash/rails.rb +0 -4
- data/tainted_hash.gemspec +2 -2
- data/test/tainted_hash_test.rb +7 -1
- metadata +2 -2
data/lib/tainted_hash.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'set'
|
2
2
|
|
3
3
|
class TaintedHash < Hash
|
4
|
-
VERSION = "0.
|
4
|
+
VERSION = "0.2.0"
|
5
5
|
|
6
6
|
class UnexposedError < StandardError
|
7
7
|
# Builds an exception when a TaintedHash has some unexposed keys. Useful
|
@@ -41,13 +41,14 @@ class TaintedHash < Hash
|
|
41
41
|
#
|
42
42
|
def initialize(hash = nil, new_class = nil)
|
43
43
|
@new_class = new_class || (hash && hash.class) || self.class.default_hash_class
|
44
|
-
|
44
|
+
@original_hash = hash || @new_class.new
|
45
|
+
@exposed_nothing = true
|
46
|
+
|
47
|
+
@original_hash.keys.each do |key|
|
45
48
|
key_s = key.to_s
|
46
49
|
next if key_s == key
|
47
50
|
@original_hash[key_s] = @original_hash.delete(key)
|
48
51
|
end
|
49
|
-
|
50
|
-
@exposed_nothing = true
|
51
52
|
end
|
52
53
|
|
53
54
|
# Public: Exposes one or more keys for the hash.
|
@@ -102,12 +103,7 @@ class TaintedHash < Hash
|
|
102
103
|
key_s = key.to_s
|
103
104
|
return if !@original_hash.key?(key_s)
|
104
105
|
|
105
|
-
|
106
|
-
when TaintedHash then value
|
107
|
-
when Hash
|
108
|
-
value = @original_hash[key_s] = self.class.new(value, @new_class)
|
109
|
-
else value
|
110
|
-
end
|
106
|
+
get_original_hash_value(key_s)
|
111
107
|
end
|
112
108
|
|
113
109
|
# Public: Attempts to set the key of a frozen hash.
|
@@ -118,11 +114,7 @@ class TaintedHash < Hash
|
|
118
114
|
# Returns nothing
|
119
115
|
def []=(key, value)
|
120
116
|
key_s = key.to_s
|
121
|
-
super(key_s,
|
122
|
-
when TaintedHash then value
|
123
|
-
when Hash then self.class.new(value, @new_class)
|
124
|
-
else value
|
125
|
-
end)
|
117
|
+
super(key_s, set_original_hash_value(key_s, value))
|
126
118
|
end
|
127
119
|
|
128
120
|
# Public: Deletes the value from both the internal and current Hash.
|
@@ -205,10 +197,27 @@ class TaintedHash < Hash
|
|
205
197
|
hash
|
206
198
|
end
|
207
199
|
|
200
|
+
def with_indifferent_access
|
201
|
+
self
|
202
|
+
end
|
203
|
+
|
208
204
|
def inspect
|
209
205
|
%(#<#{self.class}:#{object_id} @hash=#{@original_hash.inspect} @exposed=#{keys.inspect}>)
|
210
206
|
end
|
211
207
|
|
208
|
+
private
|
209
|
+
def get_original_hash_value(key_s)
|
210
|
+
set_original_hash_value(key_s, @original_hash[key_s])
|
211
|
+
end
|
212
|
+
|
213
|
+
def set_original_hash_value(key_s, value)
|
214
|
+
if value.is_a?(Hash) && !value.is_a?(TaintedHash)
|
215
|
+
value = self.class.new(value, @new_class)
|
216
|
+
end
|
217
|
+
|
218
|
+
@original_hash[key_s] = value
|
219
|
+
end
|
220
|
+
|
212
221
|
module RailsMethods
|
213
222
|
def self.included(base)
|
214
223
|
base.send :alias_method, :stringify_keys!, :stringify_keys
|
data/lib/tainted_hash/rails.rb
CHANGED
data/tainted_hash.gemspec
CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
|
|
12
12
|
## If your rubyforge_project name is different, then edit it and comment out
|
13
13
|
## the sub! line in the Rakefile
|
14
14
|
s.name = 'tainted_hash'
|
15
|
-
s.version = '0.
|
16
|
-
s.date = '2013-
|
15
|
+
s.version = '0.2.0'
|
16
|
+
s.date = '2013-07-16'
|
17
17
|
s.rubyforge_project = 'tainted_hash'
|
18
18
|
|
19
19
|
## Make sure your summary is short. The description may be as long
|
data/test/tainted_hash_test.rb
CHANGED
@@ -12,6 +12,13 @@ class TaintedHashTest < Test::Unit::TestCase
|
|
12
12
|
@tainted = TaintedHash.new @hash
|
13
13
|
end
|
14
14
|
|
15
|
+
def test_accessing_sub_hash_memoized_tainted_hash
|
16
|
+
c = @tainted[:c]
|
17
|
+
assert_kind_of TaintedHash, c
|
18
|
+
|
19
|
+
assert_equal @tainted[:c].object_id, c.object_id
|
20
|
+
end
|
21
|
+
|
15
22
|
def test_exposes_no_keys_by_default
|
16
23
|
assert !@tainted.include?('a')
|
17
24
|
assert !@tainted.include?('b')
|
@@ -182,4 +189,3 @@ class TaintedHashTest < Test::Unit::TestCase
|
|
182
189
|
assert_equal({'a' => 1}, slice.to_hash)
|
183
190
|
end
|
184
191
|
end
|
185
|
-
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tainted_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|