public
Description: Tiny module that allows you to easily adapt from one hash structure to another with a simple declarative DSL.
Homepage: http://github.com/ismasan/hash_mapper
Clone URL: git://github.com/ismasan/hash_mapper.git
ismasan (author)
Wed Jul 29 03:40:49 -0700 2009
commit  93e86186e99c8451a3747db8f745bc0cd488c246
tree    e192b070e7817628d47a3f876810a9ce74ecbe17
parent  1362045549f791df6e70ccc663c106b2e3a16696
hash_mapper / spec / hash_mapper_spec.rb
100644 431 lines (351 sloc) 9.394 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
require File.dirname(__FILE__) + '/spec_helper.rb'
 
class OneLevel
  extend HashMapper
  map from('/name'), to('/nombre')
end
 
describe 'mapping a hash wit one level' do
  
  before :each do
    @from = {:name => 'ismael'}
    @to = {:nombre => 'ismael'}
  end
  
  it "should map to" do
    OneLevel.normalize(@from).should == @to
  end
  
  it "should have indifferent access" do
    OneLevel.normalize({'name' => 'ismael'}).should == @to
  end
  
  it "should map back the other way" do
    OneLevel.denormalize(@to).should == @from
  end
  
end
 
class ManyLevels
  extend HashMapper
  map from('/name'), to('/tag_attributes/name')
  map from('/properties/type'), to('/tag_attributes/type')
  map from('/tagid'), to('/tag_id')
  map from('/properties/egg'), to('/chicken')
end
 
describe 'mapping from one nested hash to another' do
  
  before :each do
    @from = {
      :name => 'ismael',
      :tagid => 1,
      :properties => {
        :type => 'BLAH',
        :egg => 33
      }
    }
    
    @to = {
      :tag_id => 1,
      :chicken => 33,
      :tag_attributes => {
        :name => 'ismael',
        :type => 'BLAH'
      }
    }
  end
  
  it "should map from and to different depths" do
    ManyLevels.normalize(@from).should == @to
  end
  
  it "should map back the other way" do
    ManyLevels.denormalize(@to).should == @from
  end
  
end
 
class DifferentTypes
  extend HashMapper
  map from('/strings/a', &:to_s), to('/integers/a', &:to_i)
  map from('/integers/b', &:to_i), to('/strings/b', &:to_s)
end
 
describe 'coercing types' do
  
  before :each do
    @from = {
      :strings => {:a => '10'},
      :integers =>{:b => 20}
    }
    
    @to = {
      :integers => {:a => 10},
      :strings => {:b => '20'}
    }
  end
  
  it "should coerce values to specified types" do
    DifferentTypes.normalize(@from).should == @to
  end
  
  it "should coerce the other way if specified" do
    DifferentTypes.denormalize(@to).should == @from
  end
  
end
 
 
describe 'arrays in hashes' do
  before :each do
    @from = {
      :name => ['ismael','sachiyo'],
      :tagid => 1,
      :properties => {
        :type => 'BLAH',
        :egg => 33
      }
    }
    
    @to = {
      :tag_id => 1,
      :chicken => 33,
      :tag_attributes => {
        :name => ['ismael','sachiyo'],
        :type => 'BLAH'
      }
    }
  end
  
  it "should map array values as normal" do
    ManyLevels.normalize(@from).should == @to
  end
end
 
class WithArrays
  extend HashMapper
  map from('/arrays/names[0]'), to('/first_name')
  map from('/arrays/names[1]'), to('/last_name')
  map from('/arrays/company'), to('/work/company')
end
 
describe "array indexes" do
  before :each do
    @from = {
      :arrays => {
        :names => ['ismael','celis'],
        :company => 'New Bamboo'
      }
    }
    @to ={
      :first_name => 'ismael',
      :last_name => 'celis',
      :work => {:company => 'New Bamboo'}
    }
  end
  
  it "should extract defined array values" do
    pending
    WithArrays.normalize(@from).should == @to
  end
  
  it "should map the other way restoring arrays" do
    pending
    WithArrays.denormalize(@to).should == @from
  end
end
 
class PersonWithBlock
  extend HashMapper
  def self.normalize(h)
    super
  end
  map from('/names/first'){|n| n.gsub('+','')}, to('/first_name'){|n| "+++#{n}+++"}
end
class PersonWithBlockOneWay
  extend HashMapper
  map from('/names/first'), to('/first_name') do |n| "+++#{n}+++" end
end
 
describe "with blocks filters" do
  before :each do
    @from = {
      :names => {:first => 'Ismael'}
    }
    @to = {
      :first_name => '+++Ismael+++'
    }
  end
  
  it "should pass final value through given block" do
    PersonWithBlock.normalize(@from).should == @to
  end
  
  it "should be able to map the other way using a block" do
    PersonWithBlock.denormalize(@to).should == @from
  end
  
  it "should accept a block for just one direction" do
    PersonWithBlockOneWay.normalize(@from).should == @to
  end
  
end
 
class ProjectMapper
  extend HashMapper
  
  map from('/name'), to('/project_name')
  map from('/author_hash'), to('/author'), using(PersonWithBlock)
end
 
describe "with nested mapper" do
  before :each do
    @from ={
      :name => 'HashMapper',
      :author_hash => {
        :names => {:first => 'Ismael'}
      }
    }
    @to = {
      :project_name => 'HashMapper',
      :author => {:first_name => '+++Ismael+++'}
    }
  end
  
  it "should delegate nested hashes to another mapper" do
    ProjectMapper.normalize(@from).should == @to
  end
  
  it "should translate the other way using nested hashes" do
    ProjectMapper.denormalize(@to).should == @from
  end
  
end
 
class CompanyMapper
  extend HashMapper
  
  map from('/name'), to('/company_name')
  map from('/employees'), to('/employees') do |employees_array|
    employees_array.collect{|emp_hash| PersonWithBlock.normalize(emp_hash)}
  end
end
 
class CompanyEmployeesMapper
  extend HashMapper
  
  map from('/name'), to('/company_name')
  map from('/employees'), to('/employees'), using(PersonWithBlock)
end
 
describe "with arrays of nested hashes" do
  before :each do
    @from = {
      :name => 'New Bamboo',
      :employees => [
        {:names => {:first => 'Ismael'}},
        {:names => {:first => 'Sachiyo'}},
        {:names => {:first => 'Pedro'}}
      ]
    }
    @to = {
      :company_name => 'New Bamboo',
      :employees => [
        {:first_name => '+++Ismael+++'},
        {:first_name => '+++Sachiyo+++'},
        {:first_name => '+++Pedro+++'}
      ]
    }
  end
  
  it "should pass array value though given block mapper" do
    CompanyMapper.normalize(@from).should == @to
  end
  
  it "should map array elements automatically" do
    CompanyEmployeesMapper.normalize(@from).should == @to
  end
end
 
class NoKeys
  extend HashMapper
  
  map from('/exists'), to('/exists_yahoo') #in
  map from('/exists_as_nil'), to('/exists_nil') #in
  map from('/foo'), to('/bar') # not in
  
end
 
describe "with non-matching maps" do
  before :all do
    @input = {
      :exists => 1,
      :exists_as_nil => nil,
      :doesnt_exist => 2
    }
    @output = {
      :exists_yahoo => 1
    }
  end
  
  it "should ignore maps that don't exist" do
    NoKeys.normalize(@input).should == @output
  end
end
 
describe "with false values" do
  
  it "should include values in output" do
    NoKeys.normalize({'exists' => false}).should == {:exists_yahoo => false}
    NoKeys.normalize({:exists => false}).should == {:exists_yahoo => false}
  end
  
end
 
describe "with nil values" do
  
  it "should not include values in output" do
    NoKeys.normalize({:exists => nil}).should == {}
    NoKeys.normalize({'exists' => nil}).should == {}
  end
  
end
 
class WithBeforeFilters
  extend HashMapper
  map from('/hello'), to('/goodbye')
  map from('/extra'), to('/extra')
 
  before_normalize do |input, output|
    input[:extra] = "extra #{input[:hello]} innit"
    input
  end
  before_denormalize do |input, output|
    input[:goodbye] = 'changed'
    input
  end
end
 
class WithAfterFilters
  extend HashMapper
  map from('/hello'), to('/goodbye')
 
  after_normalize do |input, output|
    output = output.to_a
    output
  end
  after_denormalize do |input, output|
    output.delete(:hello)
    output
  end
end
 
describe "before and after filters" do
  before(:all) do
    @denorm = {:hello => 'wassup?!'}
    @norm = {:goodbye => 'seeya later!'}
  end
  it "should allow filtering before normalize" do
    WithBeforeFilters.normalize(@denorm).should == {:goodbye => 'wassup?!', :extra => 'extra wassup?! innit'}
  end
  it "should allow filtering before denormalize" do
    WithBeforeFilters.denormalize(@norm).should == {:hello => 'changed'}
  end
  it "should allow filtering after normalize" do
    WithAfterFilters.normalize(@denorm).should == [[:goodbye, 'wassup?!']]
  end
  it "should allow filtering after denormalize" do
    WithAfterFilters.denormalize(@norm).should == {}
  end
 
end
 
class NotRelated
  extend HashMapper
  map from('/n'), to('/n/n')
end
 
class A
  extend HashMapper
  map from('/a'), to('/a/a')
end
 
class B < A
  map from('/b'), to('/b/b')
end
 
class C < B
  map from('/c'), to('/c/c')
end
 
describe "inherited mappers" do
  before :all do
    @from = {
      :a => 'a',
      :b => 'b',
      :c => 'c'
    }
    @to_b ={
      :a => {:a => 'a'},
      :b => {:b => 'b'}
    }
 
  end
  
  it "should inherit mappings" do
    B.normalize(@from).should == @to_b
  end
  
  it "should not affect other mappers" do
    NotRelated.normalize('n' => 'nn').should == {:n => {:n => 'nn'}}
  end
end
 
class MixedMappings
  extend HashMapper
  map from('/big/jobs'), to('dodo')
  map from('/timble'), to('bingo/biscuit')
end
 
describe "dealing with strings and symbols" do
  
  it "should be able to normalize from a nested hash with string keys" do
    MixedMappings.normalize(
      'big' => {'jobs' => 5},
      'timble' => 3.2
    ).should == {:dodo => 5,
                   :bingo => {:biscuit => 3.2}}
  end
  
  it "should not symbolized keys in value hashes" do
    MixedMappings.normalize(
      'big' => {'jobs' => 5},
      'timble' => {'string key' => 'value'}
    ).should == {:dodo => 5,
                   :bingo => {:biscuit => {'string key' => 'value'}}}
  end
  
end