hash_mapper 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/hash_mapper/version.rb +1 -1
- data/lib/hash_mapper.rb +23 -14
- data/spec/hash_mapper_spec.rb +116 -0
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29ee490afe5327d30e8b443a75e6d53350bd0aa3
|
4
|
+
data.tar.gz: 41d646013d75a9c396d2c85bb3b81f81fe334c21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eae5a131bb3b95cbd4e98c771cac9ccaabb0c418356af67600f3135963add58bb8500d733db5c4b8ae74aea8035734e4c052eabb8c621a2cb09bd634669c267f
|
7
|
+
data.tar.gz: 55bbbfa2ab380e946631bbe2a58253e3d762fdea0ed737ea771cde35a72844aa06ef4015d161e762c9da19730652eb003cbc92aa1e38a5c4a8c0d1c2bdee5954
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[ ![Codeship Status for ismasan/hash_mapper](https://www.codeship.io/projects/85d172c0-4668-0132-e925-7a7d3d72b19b/status)](https://www.codeship.io/projects/45296)
|
2
|
+
|
1
3
|
# hash_mapper
|
2
4
|
|
3
5
|
* http://ismasan.github.com/hash_mapper/
|
@@ -146,7 +148,7 @@ Do this:
|
|
146
148
|
|
147
149
|
```ruby
|
148
150
|
map from('/names'), to('/user') do |names|
|
149
|
-
"Mr. #{names[
|
151
|
+
"Mr. #{names[:last]}, #{names[:first]}"
|
150
152
|
end
|
151
153
|
```
|
152
154
|
|
@@ -282,6 +284,8 @@ end
|
|
282
284
|
Important: note that for before filters, you need to return the (modified) input, and for after filters, you need to return the output.
|
283
285
|
Note also that 'output' is correct at the time of the filter, i.e. before_normalize yields 'output' as an empty hash, while after_normalize yields it as an already normalized hash.
|
284
286
|
|
287
|
+
It is possible to define multiple filters of a given type. These are run in the order in which they are defined. A common use case might be to define a `before_normalize` filter in a parent class and a child class. The output from the previous invocation of the filter is passed as the input of the next invocation.
|
288
|
+
|
285
289
|
|
286
290
|
## REQUIREMENTS:
|
287
291
|
|
data/lib/hash_mapper/version.rb
CHANGED
data/lib/hash_mapper.rb
CHANGED
@@ -42,8 +42,15 @@ module HashMapper
|
|
42
42
|
|
43
43
|
def self.extended(base)
|
44
44
|
base.class_eval do
|
45
|
-
class_attribute :maps
|
45
|
+
class_attribute :maps, :before_normalize_filters,
|
46
|
+
:before_denormalize_filters, :after_normalize_filters,
|
47
|
+
:after_denormalize_filters
|
48
|
+
|
46
49
|
self.maps = []
|
50
|
+
self.before_normalize_filters = []
|
51
|
+
self.before_denormalize_filters = []
|
52
|
+
self.after_normalize_filters = []
|
53
|
+
self.after_denormalize_filters = []
|
47
54
|
end
|
48
55
|
end
|
49
56
|
|
@@ -75,38 +82,40 @@ module HashMapper
|
|
75
82
|
end
|
76
83
|
|
77
84
|
def before_normalize(&blk)
|
78
|
-
|
85
|
+
self.before_normalize_filters = self.before_normalize_filters + [blk]
|
79
86
|
end
|
80
87
|
|
81
88
|
def before_denormalize(&blk)
|
82
|
-
|
89
|
+
self.before_denormalize_filters = self.before_denormalize_filters + [blk]
|
83
90
|
end
|
84
91
|
|
85
92
|
def after_normalize(&blk)
|
86
|
-
|
93
|
+
self.after_normalize_filters = self.after_normalize_filters + [blk]
|
87
94
|
end
|
88
95
|
|
89
96
|
def after_denormalize(&blk)
|
90
|
-
|
97
|
+
self.after_denormalize_filters = self.after_denormalize_filters + [blk]
|
91
98
|
end
|
92
99
|
|
93
100
|
protected
|
94
101
|
|
95
|
-
|
96
102
|
def perform_hash_mapping(a_hash, meth)
|
97
103
|
output = {}
|
98
|
-
|
99
|
-
|
100
|
-
a_hash =
|
104
|
+
|
105
|
+
# Before filters
|
106
|
+
a_hash = self.send(:"before_#{meth}_filters").inject(a_hash) do |memo, filter|
|
107
|
+
filter.call(memo, output)
|
108
|
+
end
|
109
|
+
|
101
110
|
# Do the mapping
|
102
111
|
self.maps.each do |m|
|
103
112
|
m.process_into(output, a_hash, meth)
|
104
113
|
end
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
114
|
+
|
115
|
+
# After filters
|
116
|
+
self.send(:"after_#{meth}_filters").inject(output) do |memo, filter|
|
117
|
+
filter.call(a_hash, memo)
|
118
|
+
end
|
110
119
|
end
|
111
120
|
|
112
121
|
# Contains PathMaps
|
data/spec/hash_mapper_spec.rb
CHANGED
@@ -447,3 +447,119 @@ describe "default values" do
|
|
447
447
|
}).should == { not_defaulted: 'some_value', defaulted: false }
|
448
448
|
end
|
449
449
|
end
|
450
|
+
|
451
|
+
class MultiBeforeFilter
|
452
|
+
extend HashMapper
|
453
|
+
|
454
|
+
before_normalize do |input, output|
|
455
|
+
input[:foo] << 'Y'
|
456
|
+
input
|
457
|
+
end
|
458
|
+
|
459
|
+
before_normalize do |input, output|
|
460
|
+
input[:foo] << 'Z'
|
461
|
+
input
|
462
|
+
end
|
463
|
+
|
464
|
+
before_denormalize do |input, output|
|
465
|
+
input[:bar].prepend('A')
|
466
|
+
input
|
467
|
+
end
|
468
|
+
|
469
|
+
before_denormalize do |input, output|
|
470
|
+
input[:bar].prepend('B')
|
471
|
+
input
|
472
|
+
end
|
473
|
+
|
474
|
+
map from('/foo'), to('bar')
|
475
|
+
end
|
476
|
+
|
477
|
+
class MultiBeforeFilterSubclass < MultiBeforeFilter
|
478
|
+
before_normalize do |input, output|
|
479
|
+
input[:foo] << '!'
|
480
|
+
input
|
481
|
+
end
|
482
|
+
|
483
|
+
before_denormalize do |input, output|
|
484
|
+
input[:bar].prepend('C')
|
485
|
+
input
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
describe 'multiple before filters' do
|
490
|
+
it 'runs before_normalize filters in the order they are defined' do
|
491
|
+
MultiBeforeFilter.normalize({ foo: 'X' }).should == { bar: 'XYZ' }
|
492
|
+
end
|
493
|
+
|
494
|
+
it 'runs before_denormalize filters in the order they are defined' do
|
495
|
+
MultiBeforeFilter.denormalize({ bar: 'X' }).should == { foo: 'BAX' }
|
496
|
+
end
|
497
|
+
|
498
|
+
context 'when the filters are spread across classes' do
|
499
|
+
it 'runs before_normalize filters in the order they are defined' do
|
500
|
+
MultiBeforeFilterSubclass.normalize({ foo: 'X' }).should == { bar: 'XYZ!' }
|
501
|
+
end
|
502
|
+
|
503
|
+
it 'runs before_denormalize filters in the order they are defined' do
|
504
|
+
MultiBeforeFilterSubclass.denormalize({ bar: 'X' }).should == { foo: 'CBAX' }
|
505
|
+
end
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
class MultiAfterFilter
|
510
|
+
extend HashMapper
|
511
|
+
|
512
|
+
map from('/baz'), to('bat')
|
513
|
+
|
514
|
+
after_normalize do |input, output|
|
515
|
+
output[:bat] << '1'
|
516
|
+
output
|
517
|
+
end
|
518
|
+
|
519
|
+
after_normalize do |input, output|
|
520
|
+
output[:bat] << '2'
|
521
|
+
output
|
522
|
+
end
|
523
|
+
|
524
|
+
after_denormalize do |input, output|
|
525
|
+
output[:baz].prepend('9')
|
526
|
+
output
|
527
|
+
end
|
528
|
+
|
529
|
+
after_denormalize do |input, output|
|
530
|
+
output[:baz].prepend('8')
|
531
|
+
output
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
class MultiAfterFilterSubclass < MultiAfterFilter
|
536
|
+
after_normalize do |input, output|
|
537
|
+
output[:bat] << '3'
|
538
|
+
output
|
539
|
+
end
|
540
|
+
|
541
|
+
after_denormalize do |input, output|
|
542
|
+
output[:baz].prepend('7')
|
543
|
+
output
|
544
|
+
end
|
545
|
+
end
|
546
|
+
|
547
|
+
describe 'multiple after filters' do
|
548
|
+
it 'runs after_normalize filters in the order they are defined' do
|
549
|
+
MultiAfterFilter.normalize({ baz: '0' }).should == { bat: '012' }
|
550
|
+
end
|
551
|
+
|
552
|
+
it 'runs after_denormalize filters in the order they are defined' do
|
553
|
+
MultiAfterFilter.denormalize({ bat: '0' }).should == { baz: '890' }
|
554
|
+
end
|
555
|
+
|
556
|
+
context 'when the filters are spread across classes' do
|
557
|
+
it 'runs after_normalize filters in the order they are defined' do
|
558
|
+
MultiAfterFilterSubclass.normalize({ baz: '0' }).should == { bat: '0123' }
|
559
|
+
end
|
560
|
+
|
561
|
+
it 'runs after_denormalize filters in the order they are defined' do
|
562
|
+
MultiAfterFilterSubclass.denormalize({ bat: '0' }).should == { baz: '7890' }
|
563
|
+
end
|
564
|
+
end
|
565
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
@@ -14,42 +14,42 @@ dependencies:
|
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Tiny module that allows you to easily adapt from one hash structure to
|
@@ -59,7 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
62
|
+
- ".gitignore"
|
63
63
|
- Gemfile
|
64
64
|
- History.txt
|
65
65
|
- README.md
|
@@ -74,25 +74,26 @@ licenses: []
|
|
74
74
|
metadata: {}
|
75
75
|
post_install_message:
|
76
76
|
rdoc_options:
|
77
|
-
- --charset=UTF-8
|
77
|
+
- "--charset=UTF-8"
|
78
78
|
require_paths:
|
79
79
|
- lib
|
80
80
|
required_ruby_version: !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|
82
|
-
- -
|
82
|
+
- - ">="
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.
|
92
|
+
rubygems_version: 2.2.2
|
93
93
|
signing_key:
|
94
94
|
specification_version: 4
|
95
95
|
summary: Maps input hashes to a normalized format
|
96
96
|
test_files:
|
97
97
|
- spec/hash_mapper_spec.rb
|
98
98
|
- spec/spec_helper.rb
|
99
|
+
has_rdoc:
|