lou 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a7d9a55a3b09cd2c50686004aff9772c89ef4b4
4
+ data.tar.gz: a04237531093015a68b9d574c179a44297281f1d
5
+ SHA512:
6
+ metadata.gz: 407f3f4f1dc67eb69414c5ebaf22e07282798c1b764dfd8abe30d09268e5767a4d9e6518ab9d87689241ba3998f433ef02da185eebeb62636b41e30998857cbe
7
+ data.tar.gz: 509306962cc37c18f6ee4bbbd6e7c43463b74a19787821829428c82c9a77f7aa841a4087136e8840b34bbd72543cd92a463ebc496b22fcbb28fc210b0ff76704
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9
4
+ - 2.0
5
+ - 2.1
6
+ - jruby-1.7
7
+ - rbx-2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lou.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Iain Beeston
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Lou
2
+ ===
3
+
4
+ [![Build Status](https://travis-ci.org/iainbeeston/lou.svg?branch=master)](https://travis-ci.org/iainbeeston/lou)
5
+ [![Code Climate](https://codeclimate.com/github/iainbeeston/lou/badges/gpa.svg)](https://codeclimate.com/github/iainbeeston/lou)
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.
8
+
9
+ Usage
10
+ -----
11
+
12
+ You can define transformations in their own class like this:
13
+
14
+ ~~~ruby
15
+ require 'lou'
16
+
17
+ class HashTransformer
18
+ extend Lou
19
+
20
+ transform forward do |x|
21
+ x.merge(a_new_key: 'this is new')
22
+ end.backward do |x|
23
+ x.delete(:a_new_key)
24
+ x
25
+ end
26
+
27
+ transform forward do |x|
28
+ x.flatten
29
+ end.backward do |x|
30
+ Hash[*x]
31
+ end
32
+ end
33
+ ~~~
34
+
35
+ Then you can use it like this:
36
+
37
+ ~~~ruby
38
+ result = HashTransformer.apply(an_old_key: 'this is old')
39
+ # [:an_old_key, "this is old", :a_new_key, "this is new"]
40
+ original = HashTransformer.undo(result)
41
+ # {:an_old_key=>"this is old"}
42
+ ~~~
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.
45
+
46
+ Credits
47
+ -------
48
+
49
+ Lou is heavily inspired by [Hash Mapper](http://github.com/ismasan) by [Ismael Celis](http://github.com/ismasan).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ module Lou
2
+ VERSION = '0.0.1'
3
+ end
data/lib/lou.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'lou/version'
2
+
3
+ module Lou
4
+ def self.extended(base)
5
+ base.class_eval do
6
+ @transforms = []
7
+ end
8
+ end
9
+
10
+ def transform(mapping)
11
+ @transforms << mapping
12
+ self
13
+ end
14
+
15
+ def apply(input)
16
+ output = deep_clone(input)
17
+ @transforms.each do |t|
18
+ output = t.apply(output)
19
+ end
20
+ output
21
+ end
22
+
23
+ def undo(output)
24
+ input = deep_clone(output)
25
+ @transforms.reverse_each do |t|
26
+ input = t.undo(input)
27
+ end
28
+ input
29
+ end
30
+
31
+ def deep_clone(obj)
32
+ Marshal.load(Marshal.dump(obj))
33
+ 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
+ end
data/lou.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lou/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lou"
8
+ spec.version = Lou::VERSION
9
+ spec.authors = ["Iain Beeston"]
10
+ spec.email = ["iain.beeston@gmail.com"]
11
+ spec.summary = %q{Flexible and reversible transforms using a declarative dsl}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^spec/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec", ">= 3.0"
23
+ end
data/spec/lou_spec.rb ADDED
@@ -0,0 +1,77 @@
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 '#undo' do
19
+ it 'returns the input' do
20
+ expect(klass.undo('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 forward { |x| x.push 'world' }.backward { |x| x.delete_if { |y| y == 'world' } }
30
+ end
31
+ end
32
+
33
+ describe '#apply' do
34
+ it 'applies the forward 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 '#undo' do
45
+ it 'applies the backward transform' do
46
+ expect(klass.undo(%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.undo(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 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\.$/, '') }
62
+ end
63
+ end
64
+
65
+ describe '#apply' do
66
+ it 'applies all of the forward 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 '#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')
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,78 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # The settings below are suggested to provide a good initial experience
19
+ # with RSpec, but feel free to customize to your heart's content.
20
+ =begin
21
+ # These two settings work together to allow you to limit a spec run
22
+ # to individual examples or groups you care about by tagging them with
23
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
24
+ # get run.
25
+ config.filter_run :focus
26
+ config.run_all_when_everything_filtered = true
27
+
28
+ # Many RSpec users commonly either run the entire suite or an individual
29
+ # file, and it's useful to allow more verbose output when running an
30
+ # individual spec file.
31
+ if config.files_to_run.one?
32
+ # Use the documentation formatter for detailed output,
33
+ # unless a formatter has already been configured
34
+ # (e.g. via a command-line flag).
35
+ config.default_formatter = 'doc'
36
+ end
37
+
38
+ # Print the 10 slowest examples and example groups at the
39
+ # end of the spec run, to help surface which specs are running
40
+ # particularly slow.
41
+ config.profile_examples = 10
42
+
43
+ # Run specs in random order to surface order dependencies. If you find an
44
+ # order dependency and want to debug it, you can fix the order by providing
45
+ # the seed, which is printed after each run.
46
+ # --seed 1234
47
+ config.order = :random
48
+
49
+ # Seed global randomization in this process using the `--seed` CLI option.
50
+ # Setting this allows you to use `--seed` to deterministically reproduce
51
+ # test failures related to randomization by passing the same `--seed` value
52
+ # as the one that triggered the failure.
53
+ Kernel.srand config.seed
54
+
55
+ # rspec-expectations config goes here. You can use an alternate
56
+ # assertion/expectation library such as wrong or the stdlib/minitest
57
+ # assertions if you prefer.
58
+ config.expect_with :rspec do |expectations|
59
+ # Enable only the newer, non-monkey-patching expect syntax.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ expectations.syntax = :expect
63
+ end
64
+
65
+ # rspec-mocks config goes here. You can use an alternate test double
66
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
67
+ config.mock_with :rspec do |mocks|
68
+ # Enable only the newer, non-monkey-patching expect syntax.
69
+ # For more details, see:
70
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ mocks.syntax = :expect
72
+
73
+ # Prevents you from mocking or stubbing a method that does not exist on
74
+ # a real object. This is generally recommended.
75
+ mocks.verify_partial_doubles = true
76
+ end
77
+ =end
78
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lou
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Iain Beeston
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - iain.beeston@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/lou.rb
70
+ - lib/lou/version.rb
71
+ - lou.gemspec
72
+ - spec/lou_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Flexible and reversible transforms using a declarative dsl
98
+ test_files:
99
+ - spec/lou_spec.rb
100
+ - spec/spec_helper.rb