ismasan-hash_mapper 0.0.6 → 0.0.7
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/lib/hash_mapper.rb +5 -14
- data/spec/hash_mapper_spec.rb +46 -1
- metadata +2 -2
data/lib/hash_mapper.rb
CHANGED
|
@@ -30,7 +30,7 @@ unless Array.instance_methods.include?("inject_with_index")
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
module HashMapper
|
|
33
|
-
VERSION = '0.0.
|
|
33
|
+
VERSION = '0.0.7'
|
|
34
34
|
|
|
35
35
|
# we need this for inheritable mappers, which is annoying because it needs ActiveSupport, kinda overkill.
|
|
36
36
|
#
|
|
@@ -91,7 +91,6 @@ module HashMapper
|
|
|
91
91
|
before_filter = instance_eval "@before_#{meth}"
|
|
92
92
|
a_hash = before_filter.call(a_hash, output) if before_filter
|
|
93
93
|
# Do the mapping
|
|
94
|
-
a_hash = symbolize_keys(a_hash)
|
|
95
94
|
maps.each do |m|
|
|
96
95
|
m.process_into(output, a_hash, meth)
|
|
97
96
|
end
|
|
@@ -102,15 +101,6 @@ module HashMapper
|
|
|
102
101
|
output
|
|
103
102
|
end
|
|
104
103
|
|
|
105
|
-
# from http://www.geekmade.co.uk/2008/09/ruby-tip-normalizing-hash-keys-as-symbols/
|
|
106
|
-
#
|
|
107
|
-
def symbolize_keys(hash)
|
|
108
|
-
hash.inject({}) do |options, (key, value)|
|
|
109
|
-
options[(key.to_sym rescue key) || key] = value
|
|
110
|
-
options
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
104
|
# Contains PathMaps
|
|
115
105
|
# Makes them interact
|
|
116
106
|
#
|
|
@@ -134,8 +124,9 @@ module HashMapper
|
|
|
134
124
|
|
|
135
125
|
def get_value_from_input(output, input, path, meth)
|
|
136
126
|
value = path.inject(input) do |h,e|
|
|
137
|
-
|
|
138
|
-
|
|
127
|
+
v = h.with_indifferent_access[e] # this does it, but uses ActiveSupport
|
|
128
|
+
throw :no_value if v.nil?#.has_key?(e)
|
|
129
|
+
v
|
|
139
130
|
end
|
|
140
131
|
delegated_mapper ? delegate_to_nested_mapper(value, meth) : value
|
|
141
132
|
end
|
|
@@ -154,7 +145,7 @@ module HashMapper
|
|
|
154
145
|
|
|
155
146
|
def add_value_to_hash!(hash, path, value)
|
|
156
147
|
path.inject_with_index(hash) do |h,e,i|
|
|
157
|
-
if h[e]
|
|
148
|
+
if !h[e].nil? # it can be FALSE
|
|
158
149
|
h[e]
|
|
159
150
|
else
|
|
160
151
|
h[e] = (i == path.size-1 ? path.apply_filter(value) : {})
|
data/spec/hash_mapper_spec.rb
CHANGED
|
@@ -294,6 +294,24 @@ describe "with non-matching maps" do
|
|
|
294
294
|
end
|
|
295
295
|
end
|
|
296
296
|
|
|
297
|
+
describe "with false values" do
|
|
298
|
+
|
|
299
|
+
it "should include values in output" do
|
|
300
|
+
NoKeys.normalize({'exists' => false}).should == {:exists_yahoo => false}
|
|
301
|
+
NoKeys.normalize({:exists => false}).should == {:exists_yahoo => false}
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
describe "with nil values" do
|
|
307
|
+
|
|
308
|
+
it "should not include values in output" do
|
|
309
|
+
NoKeys.normalize({:exists => nil}).should == {}
|
|
310
|
+
NoKeys.normalize({'exists' => nil}).should == {}
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
end
|
|
314
|
+
|
|
297
315
|
class WithBeforeFilters
|
|
298
316
|
extend HashMapper
|
|
299
317
|
map from('/hello'), to('/goodbye')
|
|
@@ -382,4 +400,31 @@ describe "inherited mappers" do
|
|
|
382
400
|
it "should not affect other mappers" do
|
|
383
401
|
NotRelated.normalize('n' => 'nn').should == {:n => {:n => 'nn'}}
|
|
384
402
|
end
|
|
385
|
-
end
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
class MixedMappings
|
|
406
|
+
extend HashMapper
|
|
407
|
+
map from('/big/jobs'), to('dodo')
|
|
408
|
+
map from('/timble'), to('bingo/biscuit')
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
describe "dealing with strings and symbols" do
|
|
412
|
+
|
|
413
|
+
it "should be able to normalize from a nested hash with string keys" do
|
|
414
|
+
MixedMappings.normalize(
|
|
415
|
+
'big' => {'jobs' => 5},
|
|
416
|
+
'timble' => 3.2
|
|
417
|
+
).should == {:dodo => 5,
|
|
418
|
+
:bingo => {:biscuit => 3.2}}
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
it "should not symbolized keys in value hashes" do
|
|
422
|
+
MixedMappings.normalize(
|
|
423
|
+
'big' => {'jobs' => 5},
|
|
424
|
+
'timble' => {'string key' => 'value'}
|
|
425
|
+
).should == {:dodo => 5,
|
|
426
|
+
:bingo => {:biscuit => {'string key' => 'value'}}}
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
end
|
|
430
|
+
|
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.7
|
|
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-
|
|
12
|
+
date: 2009-04-16 00:00:00 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|