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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a7d9a55a3b09cd2c50686004aff9772c89ef4b4
4
- data.tar.gz: a04237531093015a68b9d574c179a44297281f1d
3
+ metadata.gz: 51db60239346e259c7378b093d95fb361c6eda85
4
+ data.tar.gz: f5615ce8a5d10e09edff99d390a7457664ab39c9
5
5
  SHA512:
6
- metadata.gz: 407f3f4f1dc67eb69414c5ebaf22e07282798c1b764dfd8abe30d09268e5767a4d9e6518ab9d87689241ba3998f433ef02da185eebeb62636b41e30998857cbe
7
- data.tar.gz: 509306962cc37c18f6ee4bbbd6e7c43463b74a19787821829428c82c9a77f7aa841a4087136e8840b34bbd72543cd92a463ebc496b22fcbb28fc210b0ff76704
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 forward do |x|
20
+ transform.up do |x|
21
21
  x.merge(a_new_key: 'this is new')
22
- end.backward do |x|
22
+ end.down do |x|
23
23
  x.delete(:a_new_key)
24
24
  x
25
25
  end
26
26
 
27
- transform forward do |x|
27
+ transform.up do |x|
28
28
  x.flatten
29
- end.backward do |x|
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.undo(result)
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 ~undo~ 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.
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
@@ -1,3 +1,3 @@
1
1
  module Lou
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
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(mapping)
11
- @transforms << mapping
12
- self
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 undo(output)
25
+ def reverse(output)
24
26
  input = deep_clone(output)
25
27
  @transforms.reverse_each do |t|
26
- input = t.undo(input)
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 '#undo' do
18
+ describe '#reverse' do
19
19
  it 'returns the input' do
20
- expect(klass.undo('this is the input')).to eq('this is the input')
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 forward { |x| x.push 'world' }.backward { |x| x.delete_if { |y| y == 'world' } }
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 forward transform' do
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 '#undo' do
45
- it 'applies the backward transform' do
46
- expect(klass.undo(%w(hello world))).to eq(%w(hello))
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.undo(input) }.to_not change { input }
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 forward { |x| x + ', or not to be' }.backward { |x| x.gsub(/, or not to be$/, '') }
61
- transform forward { |x| x + ', that is the question.' }.backward { |x| x.gsub(/, that is the question\.$/, '') }
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 forward transforms in order' do
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 '#undo' do
72
- it 'applies all of the backward transforms in reverse order' do
73
- expect(klass.undo('To be, or not to be, that is the question.')).to eq('To be')
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.1
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-05 00:00:00.000000000 Z
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
- - ".gitignore"
63
- - ".rspec"
64
- - ".travis.yml"
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: []