htransform 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.3"
5
+ - "rbx-2"
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  HTransform
2
2
  ======
3
3
 
4
+ [![Build Status](https://travis-ci.org/joshkrueger/htransform.svg?branch=master)](https://travis-ci.org/joshkrueger/htransform)
5
+
4
6
  HTransform provides a simple DSL to transform a supplied hash with an arbitrary structure to another hash with an even more arbitrary structure.
5
7
 
6
- ***Note:*** *HTransform does not simply modify the hash you have passed in. It actually creates a new hash from scratch. It only copies or does transformations on the fields you specify in the transform block.*
8
+ ***Note:*** *HTransform does not simply modify the hash you have passed in. It actually creates a new hash from scratch. It only copies or does transformations on the fields you specify in the transform block, unless you use the `passthrough_remaining` option (see below)*
7
9
 
8
10
  Support
9
11
  =======
@@ -130,6 +132,16 @@ Want to combine multiple inputs when one or more are an array?
130
132
 
131
133
  input [ [ "foo", "bar" ], "baz" ] => "combined nested hash"
132
134
 
135
+ Want to simply pass some parts of your original hash through? Simply specify the keys you want passed through:
136
+
137
+ passthrough :this_one, :that_one
138
+
139
+ Want to pass every part of your original hash that was not transformed through? **HTransform CAN DO THAT TOO!** Simply add the following at the _end_ of your `transform` block:
140
+
141
+ passthrough_remaining
142
+
143
+ ***Note:*** *You MUST specify it only at the end of your `transform` block, as Tranny needs to know all the keys that were transformed before it!*
144
+
133
145
  Shorthand Limitations
134
146
  ==============
135
147
 
@@ -24,6 +24,7 @@ class HTransform
24
24
  @output_hash = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
25
25
  @input_nest = []
26
26
  @output_nest = []
27
+ @inputs = Set.new
27
28
  end
28
29
 
29
30
  def convert(input)
@@ -43,6 +44,14 @@ class HTransform
43
44
 
44
45
  private
45
46
 
47
+ def record(*keys)
48
+ keys.each{ |key| @inputs << key }
49
+ end
50
+
51
+ def recorded?(key)
52
+ @inputs.include? key
53
+ end
54
+
46
55
  def set_val(dst, val)
47
56
 
48
57
  dst = (@output_nest + [dst]).flatten unless @output_nest.empty?
@@ -118,6 +127,12 @@ class HTransform
118
127
  end
119
128
  end
120
129
 
130
+ def passthrough_remaining
131
+ @input_hash.each_key do |key|
132
+ passthrough(key) unless recorded?(key)
133
+ end
134
+ end
135
+
121
136
  def input_multiple(options)
122
137
  from, to, via, default = parse_options(options)
123
138
  via = lambda { |x| x.join(" ") } if via.nil?
@@ -139,7 +154,7 @@ class HTransform
139
154
  end
140
155
  end
141
156
 
142
-
157
+ record(*from)
143
158
  set_val(to, new_value)
144
159
  end
145
160
 
@@ -166,6 +181,7 @@ class HTransform
166
181
  get_val(from)
167
182
  end
168
183
 
184
+ record(*from)
169
185
  set_val(to, new_value)
170
186
  end
171
187
 
@@ -1,3 +1,3 @@
1
1
  class HTransform
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -367,6 +367,99 @@ describe HTransform do
367
367
 
368
368
  end
369
369
 
370
+ context "passthrough_remaining" do
371
+ it "passes all keys that were not specified in an #input through" do
372
+ class TestHTransform < HTransform
373
+ transform do
374
+ input :bar => :bar_key
375
+ passthrough :foo
376
+ passthrough_remaining
377
+ end
378
+ end
379
+
380
+ input_hash = { :bar => 'one', :foo => 1, :lala => nil, :lolo => 1 }
381
+ desired_hash = { :bar_key => 'one', :foo => 1, :lala => nil, :lolo => 1 }
382
+
383
+ result = TestHTransform.convert(input_hash)
384
+ result.should == desired_hash
385
+ end
386
+
387
+ it "treats #input_multiple keys as distinct, and does not pass them through" do
388
+ class TestHTransform < HTransform
389
+ transform do
390
+ input_multiple [:bar, :key] => :bar_key
391
+ passthrough_remaining
392
+ end
393
+ end
394
+
395
+ input_hash = { :bar => 'one', :key => 'two', :lala => nil, :lolo => 1 }
396
+ desired_hash = { :bar_key => 'one two', :lala => nil, :lolo => 1 }
397
+
398
+ result = TestHTransform.convert(input_hash)
399
+ result.should == desired_hash
400
+ end
401
+
402
+ it "treats nested inputs as distinct, and does not pass them through" do
403
+ class TestHTransform < HTransform
404
+ transform do
405
+ input [:outer, :bar] => :sandbar
406
+ passthrough_remaining
407
+ end
408
+ end
409
+
410
+ input_hash = { :outer => { :bar => 1 }, :bar => :bie, :outer2 => { :bar2 => 2 } }
411
+ desired_hash = { :sandbar => 1, :outer2 => { :bar2 => 2 } }
412
+
413
+ result = TestHTransform.convert(input_hash)
414
+ result.should == desired_hash
415
+ end
416
+
417
+ it "passes through nested outputs if they were not used as inputs" do
418
+ class TestHTransform < HTransform
419
+ transform do
420
+ input :sandbar => [:outer, :bar]
421
+ passthrough_remaining
422
+ end
423
+ end
424
+
425
+ input_hash = { :sandbar => { :inner => 1 }, :bar => :bie, :outer2 => { :bar2 => 2 } }
426
+ desired_hash = { :outer => { :bar => { :inner => 1 } }, :bar => :bie, :outer2 => { :bar2 => 2 } }
427
+
428
+ result = TestHTransform.convert(input_hash)
429
+ result.should == desired_hash
430
+ end
431
+
432
+ it "Only not passthrough the inputs in a nested-nested situation" do
433
+ class TestHTransform < HTransform
434
+ transform do
435
+ input [:sand, :bar] => [:outer, :otter]
436
+ passthrough_remaining
437
+ end
438
+ end
439
+
440
+ input_hash = { :sand => { :bar => 1 }, :bar => :bie, :otter => 2 }
441
+ desired_hash = { :outer => { :otter => 1 }, :otter => 2 }
442
+
443
+ result = TestHTransform.convert(input_hash)
444
+ result.should == desired_hash
445
+ end
446
+
447
+ it "Treats array-keys as a single entity, and passes their individual components through" do
448
+ class TestHTransform < HTransform
449
+ transform do
450
+ input [[:sand, :bar], :baz] => :combo_bar
451
+ passthrough_remaining
452
+ end
453
+ end
454
+
455
+ input_hash = { [:sand, :bar] => { :baz => 1 }, :bar => :bie, :baz => :ket, :otter => 2 }
456
+ desired_hash = { :combo_bar => 1, :bar => :bie, :otter => 2 }
457
+
458
+ result = TestHTransform.convert(input_hash)
459
+ result.should == desired_hash
460
+ end
461
+ end
462
+
370
463
  context "non-hashes" do
371
464
  it "works with objects that respond to to_hash" do
372
465
  class TestHTransform < HTransform
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htransform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-15 00:00:00.000000000 Z
12
+ date: 2014-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -52,6 +52,7 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - .gitignore
55
+ - .travis.yml
55
56
  - Gemfile
56
57
  - README.md
57
58
  - Rakefile