ismasan-hash_mapper 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -0
- data/lib/hash_mapper.rb +17 -7
- data/spec/hash_mapper_spec.rb +22 -0
- metadata +2 -2
data/README.rdoc
CHANGED
data/lib/hash_mapper.rb
CHANGED
@@ -11,7 +11,7 @@ unless Symbol.instance_methods.include?('to_proc')
|
|
11
11
|
end
|
12
12
|
|
13
13
|
module HashMapper
|
14
|
-
VERSION = '0.0.
|
14
|
+
VERSION = '0.0.4'
|
15
15
|
|
16
16
|
def maps
|
17
17
|
@maps ||= []
|
@@ -73,16 +73,26 @@ module HashMapper
|
|
73
73
|
@path_from, @path_to, @delegated_mapper = path_from, path_to, delegated_mapper
|
74
74
|
end
|
75
75
|
|
76
|
-
def process_into(output,
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
76
|
+
def process_into(output, input, meth = :normalize)
|
77
|
+
path_1, path_2 = (meth == :normalize ? [path_from, path_to] : [path_to, path_from])
|
78
|
+
catch :no_value do
|
79
|
+
value = get_value_from_input(output, input, path_1, meth)
|
80
|
+
add_value_to_hash!(output, path_2, value)
|
81
|
+
end
|
82
82
|
end
|
83
83
|
|
84
84
|
protected
|
85
85
|
|
86
|
+
def get_value_from_input(output, input, path, meth)
|
87
|
+
value = path.inject(input) do |h,e|
|
88
|
+
throw :no_value unless h.has_key?(e)
|
89
|
+
h[e]
|
90
|
+
end
|
91
|
+
value = delegate_to_nested_mapper(value, meth) if delegated_mapper
|
92
|
+
value
|
93
|
+
end
|
94
|
+
|
95
|
+
|
86
96
|
def delegate_to_nested_mapper(value, meth)
|
87
97
|
v = if value.kind_of?(Array)
|
88
98
|
value.map {|h| delegated_mapper.send(meth, h)}
|
data/spec/hash_mapper_spec.rb
CHANGED
@@ -268,4 +268,26 @@ describe "with arrays of nested hashes" do
|
|
268
268
|
end
|
269
269
|
end
|
270
270
|
|
271
|
+
class NoKeys
|
272
|
+
extend HashMapper
|
273
|
+
|
274
|
+
map from('/exists'), to('/exists_yahoo')
|
275
|
+
map from('/foo'), to('/bar')
|
276
|
+
end
|
277
|
+
|
278
|
+
describe "with non-matching maps" do
|
279
|
+
before :all do
|
280
|
+
@input = {
|
281
|
+
:exists => 1,
|
282
|
+
:doesnt_exist => 2
|
283
|
+
}
|
284
|
+
@output = {
|
285
|
+
:exists_yahoo => 1
|
286
|
+
}
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should ignore maps that don't exist" do
|
290
|
+
NoKeys.normalize(@input).should == @output
|
291
|
+
end
|
292
|
+
end
|
271
293
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ismasan-hash_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-02-
|
12
|
+
date: 2009-02-04 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|