lou 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
- data/.gemfiles/activesupport3_0.gemfile +5 -0
- data/.gemfiles/activesupport3_1.gemfile +5 -0
- data/.gemfiles/activesupport3_2.gemfile +5 -0
- data/.gemfiles/activesupport4_0.gemfile +5 -0
- data/.gemfiles/activesupport4_1.gemfile +5 -0
- data/.travis.yml +6 -0
- data/README.md +5 -5
- data/lib/lou/transformer/step.rb +23 -0
- data/lib/lou/transformer.rb +21 -9
- data/lib/lou/version.rb +1 -1
- data/lib/lou.rb +0 -31
- data/lou.gemspec +4 -0
- data/spec/lou/transformer_spec.rb +114 -0
- metadata +25 -5
- data/spec/lou_spec.rb +0 -77
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34aac868cb5bf73797d2b41155f717ee85ffe7fd
|
4
|
+
data.tar.gz: 5cc5bada947c03de042098b226b85179e394ed53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8d9d453b8dfd91ed8ad960a45086d042bedd7678d2231d99a9aa74ca5b587b825aa98c4447833a8672a0048744ac9670123524b0e788a1c5566bc9810a23984
|
7
|
+
data.tar.gz: 2451c3a54ea3c4ba91f5a3d031711882da48d34156e475e18cc1cc59bf9ed140a423630eeae252d4155b6eda5e8f095e2a40fd2721f8fb499f6fa13024310345
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -15,16 +15,16 @@ You can define transformations in their own class like this:
|
|
15
15
|
require 'lou'
|
16
16
|
|
17
17
|
class HashTransformer
|
18
|
-
extend Lou
|
18
|
+
extend Lou::Transformer
|
19
19
|
|
20
|
-
|
20
|
+
step.up do |x|
|
21
21
|
x.merge(a_new_key: 'this is new')
|
22
22
|
end.down do |x|
|
23
23
|
x.delete(:a_new_key)
|
24
24
|
x
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
step.up do |x|
|
28
28
|
x.flatten
|
29
29
|
end.down do |x|
|
30
30
|
Hash[*x]
|
@@ -41,9 +41,9 @@ original = HashTransformer.reverse(result)
|
|
41
41
|
# {:an_old_key=>"this is old"}
|
42
42
|
~~~
|
43
43
|
|
44
|
-
The transforms are applied in the order that they're defined using the ~apply~ function, with each transform receiving the result of the previous one. The process can be reversed using the ~reverse~ function.
|
44
|
+
The transforms are applied in the order that they're defined using the ~apply~ function, with each transform receiving the result of the previous one. The process can be reversed using the ~reverse~ function. Note that for each step, the input is the result of the previous step.
|
45
45
|
|
46
|
-
|
46
|
+
Transformers inherit the steps of their parent class, so it's possible to reuse steps by using inheritance.
|
47
47
|
|
48
48
|
Credits
|
49
49
|
-------
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Lou
|
2
|
+
module Transformer
|
3
|
+
class Step
|
4
|
+
def up(&block)
|
5
|
+
@up = block
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
def down(&block)
|
10
|
+
@down = block
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def apply(input)
|
15
|
+
@up.nil? ? input : @up.call(input)
|
16
|
+
end
|
17
|
+
|
18
|
+
def reverse(output)
|
19
|
+
@down.nil? ? output : @down.call(output)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/lou/transformer.rb
CHANGED
@@ -1,21 +1,33 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute'
|
2
|
+
require 'lou/transformer/step'
|
3
|
+
|
1
4
|
module Lou
|
2
|
-
|
3
|
-
def
|
4
|
-
|
5
|
-
|
5
|
+
module Transformer
|
6
|
+
def self.extended(base)
|
7
|
+
base.class_eval do
|
8
|
+
class_attribute(:steps)
|
9
|
+
self.steps = []
|
10
|
+
end
|
6
11
|
end
|
7
12
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
13
|
+
def step
|
14
|
+
Step.new.tap do |t|
|
15
|
+
steps << t
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
19
|
def apply(input)
|
14
|
-
|
20
|
+
steps.each do |t|
|
21
|
+
input = t.apply(input)
|
22
|
+
end
|
23
|
+
input
|
15
24
|
end
|
16
25
|
|
17
26
|
def reverse(output)
|
18
|
-
|
27
|
+
steps.reverse_each do |t|
|
28
|
+
output = t.reverse(output)
|
29
|
+
end
|
30
|
+
output
|
19
31
|
end
|
20
32
|
end
|
21
33
|
end
|
data/lib/lou/version.rb
CHANGED
data/lib/lou.rb
CHANGED
@@ -2,35 +2,4 @@ require 'lou/version'
|
|
2
2
|
require 'lou/transformer'
|
3
3
|
|
4
4
|
module Lou
|
5
|
-
def self.extended(base)
|
6
|
-
base.class_eval do
|
7
|
-
@transforms = []
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def transform
|
12
|
-
Transformer.new.tap do |t|
|
13
|
-
@transforms << t
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def apply(input)
|
18
|
-
output = deep_clone(input)
|
19
|
-
@transforms.each do |t|
|
20
|
-
output = t.apply(output)
|
21
|
-
end
|
22
|
-
output
|
23
|
-
end
|
24
|
-
|
25
|
-
def reverse(output)
|
26
|
-
input = deep_clone(output)
|
27
|
-
@transforms.reverse_each do |t|
|
28
|
-
input = t.reverse(input)
|
29
|
-
end
|
30
|
-
input
|
31
|
-
end
|
32
|
-
|
33
|
-
def deep_clone(obj)
|
34
|
-
Marshal.load(Marshal.dump(obj))
|
35
|
-
end
|
36
5
|
end
|
data/lou.gemspec
CHANGED
@@ -17,7 +17,11 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^spec/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
+
spec.required_ruby_version = '>= 1.9'
|
21
|
+
|
20
22
|
spec.add_development_dependency "bundler", "~> 1.6"
|
21
23
|
spec.add_development_dependency "rake"
|
22
24
|
spec.add_development_dependency "rspec", ">= 3.0"
|
25
|
+
|
26
|
+
spec.add_dependency "activesupport", ">= 3.0"
|
23
27
|
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'lou/transformer'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module Lou
|
5
|
+
describe Transformer do
|
6
|
+
context 'with no steps defined' do
|
7
|
+
let(:klass) do
|
8
|
+
Class.new do
|
9
|
+
extend Lou::Transformer
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#apply' do
|
14
|
+
it 'returns the input' do
|
15
|
+
expect(klass.apply('this is the input')).to eq('this is the input')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#reverse' do
|
20
|
+
it 'returns the input' do
|
21
|
+
expect(klass.reverse('this is the input')).to eq('this is the input')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with one step' do
|
27
|
+
let(:klass) do
|
28
|
+
Class.new do
|
29
|
+
extend Lou::Transformer
|
30
|
+
step.up { |x| x.push 'world' }.down { |x| x.delete_if { |y| y == 'world' } }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#apply' do
|
35
|
+
it 'applies the up step' do
|
36
|
+
expect(klass.apply(%w(hello))).to eq(%w(hello world))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#reverse' do
|
41
|
+
it 'applies the down step' do
|
42
|
+
expect(klass.reverse(%w(hello world))).to eq(%w(hello))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with two steps' do
|
48
|
+
let(:klass) do
|
49
|
+
Class.new do
|
50
|
+
extend Lou::Transformer
|
51
|
+
step.up { |x| x + ', or not to be' }.down { |x| x.gsub(/, or not to be$/, '') }
|
52
|
+
step.up { |x| x + ', that is the question.' }.down { |x| x.gsub(/, that is the question\.$/, '') }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#apply' do
|
57
|
+
it 'applies all of the up steps in order' do
|
58
|
+
expect(klass.apply('To be')).to eq('To be, or not to be, that is the question.')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#reverse' do
|
63
|
+
it 'applies all of the down steps in reverse order' do
|
64
|
+
expect(klass.reverse('To be, or not to be, that is the question.')).to eq('To be')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when extended from another transformer' do
|
70
|
+
let(:parent) do
|
71
|
+
Class.new do
|
72
|
+
extend Lou::Transformer
|
73
|
+
step.up { |x| x.create; x }.down { |x| x.destroy; x }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
let(:child) do
|
77
|
+
Class.new(parent) do
|
78
|
+
step.up { |x| x.create; x }.down { |x| x.destroy; x }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
let(:grandchild) do
|
82
|
+
Class.new(child) do
|
83
|
+
step.up { |x| x.create; x }.down { |x| x.destroy; x }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
let(:target) { instance_double('Target') }
|
88
|
+
|
89
|
+
describe '#apply' do
|
90
|
+
it 'applies the steps of the parent first, then the child' do
|
91
|
+
expect(target).to receive(:create).exactly(3).times
|
92
|
+
grandchild.apply(target)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'does not alter the up steps of the parent' do
|
96
|
+
expect(target).to receive(:create).exactly(1).times
|
97
|
+
parent.apply(target)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe '#reverse' do
|
102
|
+
it 'reverses the steps of the child first, then the parent' do
|
103
|
+
expect(target).to receive(:destroy).exactly(3).times
|
104
|
+
grandchild.reverse(target)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'does not alter the down steps of the parent' do
|
108
|
+
expect(target).to receive(:destroy).exactly(1).times
|
109
|
+
parent.reverse(target)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lou
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iain Beeston
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
55
69
|
description:
|
56
70
|
email:
|
57
71
|
- iain.beeston@gmail.com
|
@@ -59,6 +73,11 @@ executables: []
|
|
59
73
|
extensions: []
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
76
|
+
- .gemfiles/activesupport3_0.gemfile
|
77
|
+
- .gemfiles/activesupport3_1.gemfile
|
78
|
+
- .gemfiles/activesupport3_2.gemfile
|
79
|
+
- .gemfiles/activesupport4_0.gemfile
|
80
|
+
- .gemfiles/activesupport4_1.gemfile
|
62
81
|
- .gitignore
|
63
82
|
- .rspec
|
64
83
|
- .travis.yml
|
@@ -68,9 +87,10 @@ files:
|
|
68
87
|
- Rakefile
|
69
88
|
- lib/lou.rb
|
70
89
|
- lib/lou/transformer.rb
|
90
|
+
- lib/lou/transformer/step.rb
|
71
91
|
- lib/lou/version.rb
|
72
92
|
- lou.gemspec
|
73
|
-
- spec/
|
93
|
+
- spec/lou/transformer_spec.rb
|
74
94
|
- spec/spec_helper.rb
|
75
95
|
homepage: ''
|
76
96
|
licenses:
|
@@ -84,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
104
|
requirements:
|
85
105
|
- - '>='
|
86
106
|
- !ruby/object:Gem::Version
|
87
|
-
version: '
|
107
|
+
version: '1.9'
|
88
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
109
|
requirements:
|
90
110
|
- - '>='
|
@@ -97,5 +117,5 @@ signing_key:
|
|
97
117
|
specification_version: 4
|
98
118
|
summary: Flexible and reversible transforms using a declarative dsl
|
99
119
|
test_files:
|
100
|
-
- spec/
|
120
|
+
- spec/lou/transformer_spec.rb
|
101
121
|
- spec/spec_helper.rb
|
data/spec/lou_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'lou'
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe Lou do
|
5
|
-
context 'with no transformations defined' do
|
6
|
-
let(:klass) do
|
7
|
-
Class.new do
|
8
|
-
extend Lou
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe '#apply' do
|
13
|
-
it 'returns the input' do
|
14
|
-
expect(klass.apply('this is the input')).to eq('this is the input')
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '#reverse' do
|
19
|
-
it 'returns the input' do
|
20
|
-
expect(klass.reverse('this is the input')).to eq('this is the input')
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'with one transform' do
|
26
|
-
let(:klass) do
|
27
|
-
Class.new do
|
28
|
-
extend Lou
|
29
|
-
transform.up { |x| x.push 'world' }.down { |x| x.delete_if { |y| y == 'world' } }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe '#apply' do
|
34
|
-
it 'applies the up transform' do
|
35
|
-
expect(klass.apply(%w(hello))).to eq(%w(hello world))
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'does not change the input object' do
|
39
|
-
input = %w(hello)
|
40
|
-
expect { klass.apply(input) }.to_not change { input }
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe '#reverse' do
|
45
|
-
it 'applies the down transform' do
|
46
|
-
expect(klass.reverse(%w(hello world))).to eq(%w(hello))
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'does not change the input object' do
|
50
|
-
input = %w(hello world)
|
51
|
-
expect { klass.reverse(input) }.to_not change { input }
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'with two transforms' do
|
57
|
-
let(:klass) do
|
58
|
-
Class.new do
|
59
|
-
extend Lou
|
60
|
-
transform.up { |x| x + ', or not to be' }.down { |x| x.gsub(/, or not to be$/, '') }
|
61
|
-
transform.up { |x| x + ', that is the question.' }.down { |x| x.gsub(/, that is the question\.$/, '') }
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
describe '#apply' do
|
66
|
-
it 'applies all of the up transforms in order' do
|
67
|
-
expect(klass.apply('To be')).to eq('To be, or not to be, that is the question.')
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe '#reverse' do
|
72
|
-
it 'applies all of the down transforms in reverse order' do
|
73
|
-
expect(klass.reverse('To be, or not to be, that is the question.')).to eq('To be')
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|