lou 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/lib/lou/transformer.rb +21 -0
- data/lib/lou/version.rb +1 -1
- data/lib/lou.rb +7 -33
- data/spec/lou_spec.rb +14 -14
- metadata +14 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51db60239346e259c7378b093d95fb361c6eda85
|
4
|
+
data.tar.gz: f5615ce8a5d10e09edff99d390a7457664ab39c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 564ad4209e2dbfecc4e251a4d85008462110faed41f0fc85973179ef73c5da2984dacc7d947cf873061898c3ef67ce86192519962081155622a0a9f1a1591863
|
7
|
+
data.tar.gz: 241c01bfa29c24bd457570534887aa1ba258918605b9480abff172adeb4d7dafd2b0dfc29c92d30e123461c79a7558f2ca9233b04fc5610bdbb224603513ebec
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Lou
|
|
4
4
|
[![Build Status](https://travis-ci.org/iainbeeston/lou.svg?branch=master)](https://travis-ci.org/iainbeeston/lou)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/iainbeeston/lou/badges/gpa.svg)](https://codeclimate.com/github/iainbeeston/lou)
|
6
6
|
|
7
|
-
Lou lets you define a pipeline of reversible transformations, that you can apply to any ruby object. For example, you might want to define a pipeline of [ImageMagick](http://www.imagemagick.org) operations on an image, or a sequence of API calls.
|
7
|
+
Lou lets you define a pipeline of reversible transformations, that you can apply to any ruby object. It assumes nothing about your business logic or the objects that you're using. For example, you might want to define a pipeline of [ImageMagick](http://www.imagemagick.org) operations on an image, or a sequence of API calls. You could even use Lou as a replacement for ActiveRecord migrations.
|
8
8
|
|
9
9
|
Usage
|
10
10
|
-----
|
@@ -17,16 +17,16 @@ require 'lou'
|
|
17
17
|
class HashTransformer
|
18
18
|
extend Lou
|
19
19
|
|
20
|
-
transform
|
20
|
+
transform.up do |x|
|
21
21
|
x.merge(a_new_key: 'this is new')
|
22
|
-
end.
|
22
|
+
end.down do |x|
|
23
23
|
x.delete(:a_new_key)
|
24
24
|
x
|
25
25
|
end
|
26
26
|
|
27
|
-
transform
|
27
|
+
transform.up do |x|
|
28
28
|
x.flatten
|
29
|
-
end.
|
29
|
+
end.down do |x|
|
30
30
|
Hash[*x]
|
31
31
|
end
|
32
32
|
end
|
@@ -37,11 +37,13 @@ Then you can use it like this:
|
|
37
37
|
~~~ruby
|
38
38
|
result = HashTransformer.apply(an_old_key: 'this is old')
|
39
39
|
# [:an_old_key, "this is old", :a_new_key, "this is new"]
|
40
|
-
original = HashTransformer.
|
40
|
+
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 ~
|
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.
|
45
|
+
|
46
|
+
Note that for each transform, the input is the result of the previous step.
|
45
47
|
|
46
48
|
Credits
|
47
49
|
-------
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Lou
|
2
|
+
class Transformer
|
3
|
+
def up(&block)
|
4
|
+
@up = block
|
5
|
+
self
|
6
|
+
end
|
7
|
+
|
8
|
+
def down(&block)
|
9
|
+
@down = block
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def apply(input)
|
14
|
+
@up.nil? ? input : @up.call(input)
|
15
|
+
end
|
16
|
+
|
17
|
+
def reverse(output)
|
18
|
+
@down.nil? ? output : @down.call(output)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/lou/version.rb
CHANGED
data/lib/lou.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'lou/version'
|
2
|
+
require 'lou/transformer'
|
2
3
|
|
3
4
|
module Lou
|
4
5
|
def self.extended(base)
|
@@ -7,9 +8,10 @@ module Lou
|
|
7
8
|
end
|
8
9
|
end
|
9
10
|
|
10
|
-
def transform
|
11
|
-
|
12
|
-
|
11
|
+
def transform
|
12
|
+
Transformer.new.tap do |t|
|
13
|
+
@transforms << t
|
14
|
+
end
|
13
15
|
end
|
14
16
|
|
15
17
|
def apply(input)
|
@@ -20,10 +22,10 @@ module Lou
|
|
20
22
|
output
|
21
23
|
end
|
22
24
|
|
23
|
-
def
|
25
|
+
def reverse(output)
|
24
26
|
input = deep_clone(output)
|
25
27
|
@transforms.reverse_each do |t|
|
26
|
-
input = t.
|
28
|
+
input = t.reverse(input)
|
27
29
|
end
|
28
30
|
input
|
29
31
|
end
|
@@ -31,32 +33,4 @@ module Lou
|
|
31
33
|
def deep_clone(obj)
|
32
34
|
Marshal.load(Marshal.dump(obj))
|
33
35
|
end
|
34
|
-
|
35
|
-
def forward(&block)
|
36
|
-
Transformer.new(&block)
|
37
|
-
end
|
38
|
-
|
39
|
-
class Transformer
|
40
|
-
def initialize(&block)
|
41
|
-
forward(&block)
|
42
|
-
end
|
43
|
-
|
44
|
-
def forward(&block)
|
45
|
-
@forward = block
|
46
|
-
self
|
47
|
-
end
|
48
|
-
|
49
|
-
def backward(&block)
|
50
|
-
@backward = block
|
51
|
-
self
|
52
|
-
end
|
53
|
-
|
54
|
-
def apply(input)
|
55
|
-
@forward.nil? ? input : @forward.call(input)
|
56
|
-
end
|
57
|
-
|
58
|
-
def undo(output)
|
59
|
-
@backward.nil? ? output : @backward.call(output)
|
60
|
-
end
|
61
|
-
end
|
62
36
|
end
|
data/spec/lou_spec.rb
CHANGED
@@ -15,9 +15,9 @@ describe Lou do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
describe '#
|
18
|
+
describe '#reverse' do
|
19
19
|
it 'returns the input' do
|
20
|
-
expect(klass.
|
20
|
+
expect(klass.reverse('this is the input')).to eq('this is the input')
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -26,12 +26,12 @@ describe Lou do
|
|
26
26
|
let(:klass) do
|
27
27
|
Class.new do
|
28
28
|
extend Lou
|
29
|
-
transform
|
29
|
+
transform.up { |x| x.push 'world' }.down { |x| x.delete_if { |y| y == 'world' } }
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
describe '#apply' do
|
34
|
-
it 'applies the
|
34
|
+
it 'applies the up transform' do
|
35
35
|
expect(klass.apply(%w(hello))).to eq(%w(hello world))
|
36
36
|
end
|
37
37
|
|
@@ -41,14 +41,14 @@ describe Lou do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
describe '#
|
45
|
-
it 'applies the
|
46
|
-
expect(klass.
|
44
|
+
describe '#reverse' do
|
45
|
+
it 'applies the down transform' do
|
46
|
+
expect(klass.reverse(%w(hello world))).to eq(%w(hello))
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'does not change the input object' do
|
50
50
|
input = %w(hello world)
|
51
|
-
expect { klass.
|
51
|
+
expect { klass.reverse(input) }.to_not change { input }
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -57,20 +57,20 @@ describe Lou do
|
|
57
57
|
let(:klass) do
|
58
58
|
Class.new do
|
59
59
|
extend Lou
|
60
|
-
transform
|
61
|
-
transform
|
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
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
describe '#apply' do
|
66
|
-
it 'applies all of the
|
66
|
+
it 'applies all of the up transforms in order' do
|
67
67
|
expect(klass.apply('To be')).to eq('To be, or not to be, that is the question.')
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
describe '#
|
72
|
-
it 'applies all of the
|
73
|
-
expect(klass.
|
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
74
|
end
|
75
75
|
end
|
76
76
|
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lou
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.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-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '3.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
55
|
description:
|
@@ -59,14 +59,15 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
65
|
- Gemfile
|
66
66
|
- LICENSE.txt
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- lib/lou.rb
|
70
|
+
- lib/lou/transformer.rb
|
70
71
|
- lib/lou/version.rb
|
71
72
|
- lou.gemspec
|
72
73
|
- spec/lou_spec.rb
|
@@ -81,12 +82,12 @@ require_paths:
|
|
81
82
|
- lib
|
82
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
84
|
requirements:
|
84
|
-
- -
|
85
|
+
- - '>='
|
85
86
|
- !ruby/object:Gem::Version
|
86
87
|
version: '0'
|
87
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
|
-
- -
|
90
|
+
- - '>='
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
92
93
|
requirements: []
|