ismasan-hash_mapper 0.0.3 → 0.0.4

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.
@@ -206,6 +206,11 @@ Do this:
206
206
 
207
207
  sudo gem install ismasan-hash_mapper
208
208
 
209
+ == Credits:
210
+
211
+ * Ismael Celis (http://www.estadobeta.com)
212
+ * Mark Evans
213
+
209
214
  == LICENSE:
210
215
 
211
216
  (The MIT License)
@@ -11,7 +11,7 @@ unless Symbol.instance_methods.include?('to_proc')
11
11
  end
12
12
 
13
13
  module HashMapper
14
- VERSION = '0.0.3'
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, incoming_hash, meth = :normalize)
77
- paths = [path_from, path_to]
78
- paths.reverse! unless meth == :normalize
79
- value = paths.first.inject(incoming_hash){|h,e| h[e]}
80
- value = delegate_to_nested_mapper(value, meth) if delegated_mapper
81
- add_value_to_hash!(output, paths.last, value)
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)}
@@ -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.3
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-03 00:00:00 -08:00
12
+ date: 2009-02-04 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency