hash_mapper 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29ee490afe5327d30e8b443a75e6d53350bd0aa3
4
- data.tar.gz: 41d646013d75a9c396d2c85bb3b81f81fe334c21
3
+ metadata.gz: af39a6882d3304ec2e61725f82386d1856852971
4
+ data.tar.gz: bdd2c354eb2537699d33501240911f799b18dabc
5
5
  SHA512:
6
- metadata.gz: eae5a131bb3b95cbd4e98c771cac9ccaabb0c418356af67600f3135963add58bb8500d733db5c4b8ae74aea8035734e4c052eabb8c621a2cb09bd634669c267f
7
- data.tar.gz: 55bbbfa2ab380e946631bbe2a58253e3d762fdea0ed737ea771cde35a72844aa06ef4015d161e762c9da19730652eb003cbc92aa1e38a5c4a8c0d1c2bdee5954
6
+ metadata.gz: aff0e443feb90e2a54325138e4660922a89351a510fb947295dd855ab219599909c9d643f8ae355f8c93c296c8514faf9e82b4d506d341b5c8dd445429ebb853
7
+ data.tar.gz: 55176739edcd3034c6f57c765d3b68f7a5b16ea3f2b0207d19dd5a70cc27c56f3ee089a9dc306ab3015d7467ee5c3a000c6b7e9135897fa131d5ef789166fa25
data/README.md CHANGED
@@ -286,6 +286,26 @@ Note also that 'output' is correct at the time of the filter, i.e. before_normal
286
286
 
287
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
288
 
289
+ You can pass one extra argument to before and after filters if you need to:
290
+ ```ruby
291
+ class EggMapper
292
+ map from('/raw'), to('/fried')
293
+
294
+ before_normalize do |input, output, opts|
295
+ input['raw'] ||= 'please' unless opts[:no_default] # this will give 'raw' a default value
296
+ input
297
+ end
298
+
299
+ after_denormalize do |input, output, opts|
300
+ output.to_a # the denormalized object will now be an array, not a hash!!
301
+ end
302
+
303
+ end
304
+
305
+ EggMapper.normalize({}, no_default: true)
306
+ EggMapper.denormalize({fried: 4})
307
+ ```
308
+
289
309
 
290
310
  ## REQUIREMENTS:
291
311
 
@@ -308,6 +328,7 @@ It is possible to define multiple filters of a given type. These are run in the
308
328
  * Jdeveloper (Contributor - http://github.com/jdeveloper)
309
329
  * nightscape (Contributor - http://github.com/nightscape)
310
330
  * radamanthus (Contributor - http://github.com/radamanthus)
331
+ * Tom Wey (Contributor - (https://github.com/tjmw)
311
332
 
312
333
  ## LICENSE:
313
334
 
@@ -73,12 +73,12 @@ module HashMapper
73
73
  { using: mapper_class }
74
74
  end
75
75
 
76
- def normalize(a_hash)
77
- perform_hash_mapping a_hash, :normalize
76
+ def normalize(a_hash, opts = {})
77
+ perform_hash_mapping a_hash, :normalize, opts
78
78
  end
79
79
 
80
- def denormalize(a_hash)
81
- perform_hash_mapping a_hash, :denormalize
80
+ def denormalize(a_hash, opts = {})
81
+ perform_hash_mapping a_hash, :denormalize, opts
82
82
  end
83
83
 
84
84
  def before_normalize(&blk)
@@ -99,12 +99,12 @@ module HashMapper
99
99
 
100
100
  protected
101
101
 
102
- def perform_hash_mapping(a_hash, meth)
102
+ def perform_hash_mapping(a_hash, meth, opts)
103
103
  output = {}
104
104
 
105
105
  # Before filters
106
106
  a_hash = self.send(:"before_#{meth}_filters").inject(a_hash) do |memo, filter|
107
- filter.call(memo, output)
107
+ filter.call(memo, output, opts)
108
108
  end
109
109
 
110
110
  # Do the mapping
@@ -114,7 +114,7 @@ module HashMapper
114
114
 
115
115
  # After filters
116
116
  self.send(:"after_#{meth}_filters").inject(output) do |memo, filter|
117
- filter.call(a_hash, memo)
117
+ filter.call(a_hash, memo, opts)
118
118
  end
119
119
  end
120
120
 
@@ -1,3 +1,3 @@
1
1
  module HashMapper
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -563,3 +563,43 @@ describe 'multiple after filters' do
563
563
  end
564
564
  end
565
565
  end
566
+
567
+ class WithOptions
568
+ extend HashMapper
569
+
570
+ before_normalize do |input, output, opts|
571
+ output[:bn] = opts[:bn] if opts[:bn]
572
+ input
573
+ end
574
+
575
+ after_normalize do |input, output, opts|
576
+ output[:an] = opts[:an] if opts[:an]
577
+ output
578
+ end
579
+
580
+ before_denormalize do |input, output, opts|
581
+ output[:bdn] = opts[:bdn] if opts[:bdn]
582
+ input
583
+ end
584
+
585
+ after_denormalize do |input, output, opts|
586
+ output[:adn] = opts[:adn] if opts[:adn]
587
+ output
588
+ end
589
+ end
590
+
591
+ describe 'with options' do
592
+ context 'when called with options' do
593
+ it 'passes the options to all the filters' do
594
+ WithOptions.normalize({}, bn: 1, an: 2).should == {bn: 1, an: 2}
595
+ WithOptions.denormalize({}, bdn: 1, adn: 2).should == {bdn: 1, adn: 2}
596
+ end
597
+ end
598
+
599
+ context 'when called without options' do
600
+ it 'stills work' do
601
+ WithOptions.normalize({}).should == {}
602
+ WithOptions.denormalize({}).should == {}
603
+ end
604
+ end
605
+ 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.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
@@ -89,11 +89,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.2.2
92
+ rubygems_version: 2.4.5
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: