shapeshifter 0.0.1 → 0.0.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: 24ea440ce974a1b51f22398d4e9d11948208a12c
4
- data.tar.gz: 4317404d75d4ee54caddff085b1cec73d4bf5a61
3
+ metadata.gz: 14c8515972508e77d7733808003b3868fbcac5cc
4
+ data.tar.gz: 333baf056a7df6e54604f7687458e30da4e3af93
5
5
  SHA512:
6
- metadata.gz: 9a1480841cec56239fc71df4b2bd9ca3cc861ee4be9a005c6e446105fb40d43f8421bf18160b2c714bfed9f2c3f5b36f0b4e412b86728dabd5bb88d00d9cc4b0
7
- data.tar.gz: b64cc78d67df860e33c7918060f08c416c262cd46c33293b10574833635d269cba2a297d03dc2a84a1102af5ddd9be4a3406423fe56a2bc6dc18fbb17724a65a
6
+ metadata.gz: eedf79536b7d0a84901371b05d8a0ea17540e6e9ebfe2c650ef5f55f23f5d142111a9a26c10eca1779eec650cb059def453b6b39a6aaa834acd5929fe560fe2d
7
+ data.tar.gz: 1bc7a8d13c0b36e43f459f18b2b133ec1c24c8a9f69eeecb067e113796f47752686af7976bc4fb00643027c167184d73dccb00139750dc945c96cf17e2ac8cde
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Shapeshifter
2
2
 
3
- TODO: Write a gem description
3
+ Shapeshifter is a simple pattern for transforming data from
4
+ one format to another.
4
5
 
5
6
  ## Installation
6
7
 
@@ -20,7 +21,91 @@ Or install it yourself as:
20
21
 
21
22
  ## Usage
22
23
 
23
- TODO: Write usage instructions here
24
+ #A Simple Shifter
25
+
26
+ ```ruby
27
+ class SimpleShifter < Shapeshifter::Shifter
28
+ def shift(target_object)
29
+ # Transform target_object using source_object
30
+ targeting_object # Return the transformed target
31
+ end
32
+
33
+ def revert(target_object)
34
+ # Transform target_object using source_object
35
+ targeting_object # Return the transformed target
36
+ end
37
+ end
38
+ ```
39
+
40
+ A shifter consists of two methods: `shift` and `revert`. Both
41
+ follow the same pattern in that the shifter is instantiated with
42
+ a source object and each method receives a target object to manipulate
43
+ using the data of the source.
44
+
45
+ #Chaining
46
+
47
+ The shifter by itself is not particularly interesting, but where it
48
+ gets fun is that they can be chained together to break complex
49
+ data manipulations down into simple small chunks.
50
+
51
+
52
+ ```ruby
53
+ shift_chain = SimpleShifter
54
+ .chain(SimpleShifter2)
55
+ .chain(SimpleShifter3)
56
+ ```
57
+
58
+ This shift chain can then be easily reused for both shifting 'forward'
59
+ and reverting 'backwards'.
60
+
61
+ ```ruby
62
+ shift_chain.shift(source_object, target_object)
63
+ shift_chain.revert(source_object, target_object)
64
+ ```
65
+
66
+ When running a forward shift the shifters will be run in
67
+ sequence `SimpleShifter -> SimpleShifter2 -> SimpleShifter3` each
68
+ being instantiated with the source object (then available as
69
+ an `attr_reader` within the instance) and then being sent the
70
+ `shift` message with the target object as an argument. The target object
71
+ will therefore change as it passes through the chain allowing you
72
+ to have shifters that operate differently based on its contents.
73
+
74
+ Revert operates in the same fashion except it runs through the
75
+ chain in reverse, starting at `SimpleShifter3` and finishing at
76
+ `SimpleShifter` calling the revert method on each instance as
77
+ it traverses the chain.
78
+
79
+ #Nesting
80
+
81
+ One of the nice side effects is that you can nest shifters calls
82
+ within other shifters, e.g.
83
+
84
+ ```ruby
85
+ class ComplexShifter < Shapeshifter::Shifter
86
+ def shift(target_object)
87
+ sub_object = source_object.sub_object
88
+ partial_target = internal_chain.shift(sub_object, {})
89
+ target_object.merge(partial_target: partial_target)
90
+ end
91
+
92
+ def revert(target_object)
93
+ #...
94
+ internal_chain.revert(sub_object, {})
95
+ #...
96
+ target_object
97
+ end
98
+
99
+ def internal_chain
100
+ SimpleShifter
101
+ .chain(SimpleShifter2)
102
+ .chain(SimpleShifter3)
103
+ end
104
+ end
105
+ ```
106
+
107
+ This allows you to build quite complex data manipulations in small
108
+ easily testable chunks.
24
109
 
25
110
  ## Contributing
26
111
 
data/lib/shapeshifter.rb CHANGED
@@ -5,4 +5,3 @@ end
5
5
 
6
6
  require "shapeshifter/shifter"
7
7
  require "shapeshifter/shift_chain"
8
- require 'shapeshifter/null_shifter'
@@ -20,6 +20,7 @@ module Shapeshifter
20
20
  end
21
21
  end
22
22
 
23
+ # :nocov:
23
24
  def shift(_)
24
25
  raise NoMethodError.new('Should be overridden')
25
26
  end
@@ -27,5 +28,6 @@ module Shapeshifter
27
28
  def revert(_)
28
29
  raise NoMethodError.new('Should be overridden')
29
30
  end
31
+ # :nocov:
30
32
  end
31
33
  end
@@ -1,3 +1,3 @@
1
1
  module Shapeshifter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/shapeshifter.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["max@driftrock.com"]
11
11
  spec.summary = %q{Shapeshifter allows you to transform data types}
12
12
  spec.description = %q{}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/Driftrock/shapeshifter"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -28,6 +28,24 @@ module Shapeshifter
28
28
  end
29
29
  end
30
30
 
31
+ context 'when a reversion is made' do
32
+ before do
33
+ test_shifter = double(TestShifter)
34
+ allow(TestShifter).to receive(:new).and_return(test_shifter)
35
+ allow(test_shifter).to receive(:revert) do |target|
36
+ target[:a] = Array(target[:a]).concat(target[:b])
37
+ target
38
+ end
39
+ end
40
+
41
+ it 'should change the passed target' do
42
+ target = { a: 1, b: [2, 3] }
43
+ expect do
44
+ TestShifter.revert({}, target)
45
+ end.to change { target }.to eq ({ a: [1, 2, 3], b: [2, 3] })
46
+ end
47
+ end
48
+
31
49
  context 'when a shift is made' do
32
50
  before do
33
51
  test_shifter = double(TestShifter)
@@ -45,11 +63,5 @@ module Shapeshifter
45
63
  end.to change { target }.to eq ({ a: [1, 2, 3], b: [2, 3] })
46
64
  end
47
65
  end
48
- #
49
- #class TestShifter < Shifter
50
- # def shift(old_object, new_object)
51
- # new_oib
52
- # end
53
- #end
54
66
  end
55
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shapeshifter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max
@@ -94,7 +94,6 @@ files:
94
94
  - README.md
95
95
  - Rakefile
96
96
  - lib/shapeshifter.rb
97
- - lib/shapeshifter/null_shifter.rb
98
97
  - lib/shapeshifter/shift_chain.rb
99
98
  - lib/shapeshifter/shifter.rb
100
99
  - lib/shapeshifter/version.rb
@@ -103,7 +102,7 @@ files:
103
102
  - spec/fixtures/test_shifter2.rb
104
103
  - spec/shapeshifter/shifter_spec.rb
105
104
  - spec/spec_helper.rb
106
- homepage: ''
105
+ homepage: https://github.com/Driftrock/shapeshifter
107
106
  licenses:
108
107
  - MIT
109
108
  metadata: {}
@@ -1,7 +0,0 @@
1
- module Shapeshifter
2
- class NullShifter < Shifter
3
- def shift(res)
4
- res
5
- end
6
- end
7
- end