hash_transformer 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/README.rdoc +5 -2
- data/VERSION +1 -1
- data/lib/hash_transformer.rb +18 -0
- data/spec/hash_transformer_spec.rb +11 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -8,10 +8,13 @@ applies a sequence of transforms to a Hash to produce a new Hash
|
|
8
8
|
delete_key :foo
|
9
9
|
map_key :bar, :baz
|
10
10
|
map_key :foobar, :foobaz
|
11
|
+
transform_hash :boo do
|
12
|
+
map_key :foofoo, :barbar
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
|
-
t.transform(:foo=>1, :bar=>2, :foobar=>3)
|
14
|
-
=> {:baz=>2, :foobaz=>3}
|
16
|
+
t.transform(:foo=>1, :bar=>2, :foobar=>3, :boo=>{:foofoo=>5})
|
17
|
+
=> {:baz=>2, :foobaz=>3, :boo=>{:barbar=>5}}
|
15
18
|
|
16
19
|
== Note on Patches/Pull Requests
|
17
20
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/hash_transformer.rb
CHANGED
@@ -18,6 +18,10 @@ class HashTransformer
|
|
18
18
|
def map_key(key, to)
|
19
19
|
transforms << MapKeyTransform.new(key, to)
|
20
20
|
end
|
21
|
+
|
22
|
+
def transform_hash(key, &proc)
|
23
|
+
transforms << HashTransformerTransform.new(key, &proc)
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
class DeleteKeyTransform
|
@@ -52,6 +56,20 @@ class HashTransformer
|
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
59
|
+
class HashTransformerTransform
|
60
|
+
attr_reader :key
|
61
|
+
attr_reader :hash_transformer
|
62
|
+
|
63
|
+
def initialize(key, &proc)
|
64
|
+
@key = key
|
65
|
+
@hash_transformer = HashTransformer.new(&proc)
|
66
|
+
end
|
67
|
+
|
68
|
+
def transform(h)
|
69
|
+
h[key] = @hash_transformer.transform(h[key])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
55
73
|
attr_reader :transforms
|
56
74
|
|
57
75
|
def initialize(&proc)
|
@@ -23,4 +23,15 @@ describe "HashTransformer" do
|
|
23
23
|
}.should raise_error(RuntimeError)
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
describe "HashTransformerTransform" do
|
28
|
+
it "should transform a hash-valued key" do
|
29
|
+
t = HashTransformer.new do
|
30
|
+
transform_hash :foo do
|
31
|
+
map_key :foofoo, :barbar
|
32
|
+
end
|
33
|
+
end
|
34
|
+
t.transform(:foo=>{:foofoo=>1}).should == {:foo=>{:barbar=>1}}
|
35
|
+
end
|
36
|
+
end
|
26
37
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_transformer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mccraigmccraig of the clan mccraig
|