cooperator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/cooperator.gemspec +23 -0
- data/lib/cooperator/context.rb +57 -0
- data/lib/cooperator/version.rb +3 -0
- data/lib/cooperator.rb +58 -0
- data/spec/context.rb +57 -0
- data/spec/cooperate.rb +77 -0
- data/spec/cooperator.rb +47 -0
- data/spec/failure.rb +32 -0
- data/spec/success.rb +59 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6671fcd41b0cea041ba1f0191cd5760d1cf2bab3
|
4
|
+
data.tar.gz: 9beb667615a70908cecba636f6ae3b0a80d9d119
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e2779ddbd074fc07515eeb3ec9494f2367463627748826df8a71747eacb1dcd96c12e5e253ba94cb7d77ba79f43cfca38619af734c60b317fbf3d40c1db18fd3
|
7
|
+
data.tar.gz: fcc74dba7a8c3e156ffd3095a8c521b41fb22c26a42bba8054dd0e38f393b130d9bb9c1a195b452d643bb726610777b1b46b97d661da08735dbf9f4bf6236e8a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Erol Fornoles
|
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,37 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/Erol/cooperator.png)](https://codeclimate.com/github/Erol/cooperator)
|
2
|
+
|
3
|
+
# Cooperator
|
4
|
+
|
5
|
+
Simple cooperative interactors for Ruby
|
6
|
+
|
7
|
+
Inspired by the following:
|
8
|
+
|
9
|
+
* [LightService](https://github.com/adomokos/light-service) by [Atilla Domokos](https://github.com/adomokos)
|
10
|
+
* [Interactor](https://github.com/collectiveidea/interactor) from [Collective Idea](https://github.com/collectiveidea)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'cooperator'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install cooperator
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
TODO: Write usage instructions here
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch ( `git checkout -b my-new-feature` )
|
34
|
+
3. Create tests and make them pass ( `rake test` )
|
35
|
+
4. Commit your changes ( `git commit -am 'Added some feature'` )
|
36
|
+
5. Push to the branch ( `git push origin my-new-feature` )
|
37
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/cooperator.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 'cooperator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'cooperator'
|
8
|
+
spec.version = Cooperator::VERSION
|
9
|
+
spec.authors = ['Erol Fornoles']
|
10
|
+
spec.email = ['erol.fornoles@gmail.com']
|
11
|
+
spec.summary = %q{Simple cooperative interactors for Ruby}
|
12
|
+
spec.description = %q{Simple cooperative interactors for Ruby}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', "~> 1.5"
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Cooperator
|
2
|
+
class Context
|
3
|
+
def initialize(attributes = {})
|
4
|
+
@_attributes = {_failure: false}
|
5
|
+
|
6
|
+
attributes.each do |k, v|
|
7
|
+
send :"#{k}=", v
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def errors
|
12
|
+
@_errors ||= []
|
13
|
+
end
|
14
|
+
|
15
|
+
def success!
|
16
|
+
self._failure = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure!(*args)
|
20
|
+
args.each do |error|
|
21
|
+
errors.push error
|
22
|
+
end
|
23
|
+
|
24
|
+
self._failure = true
|
25
|
+
end
|
26
|
+
|
27
|
+
def success?
|
28
|
+
not failure?
|
29
|
+
end
|
30
|
+
|
31
|
+
def failure?
|
32
|
+
_failure
|
33
|
+
end
|
34
|
+
|
35
|
+
def include?(key)
|
36
|
+
@_attributes.include? key
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing(method, *args, &block)
|
40
|
+
return @_attributes.fetch method if @_attributes.include? method
|
41
|
+
|
42
|
+
name = String method
|
43
|
+
|
44
|
+
if name.include? '='
|
45
|
+
name.gsub!(/=/, '')
|
46
|
+
|
47
|
+
@_attributes[:"#{name}"] = args.shift
|
48
|
+
else
|
49
|
+
super
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def respond_to_missing?(method, private = false)
|
54
|
+
@_attributes.include?(method) || super
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/cooperator.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'cooperator/version'
|
2
|
+
require 'cooperator/context'
|
3
|
+
|
4
|
+
module Cooperator
|
5
|
+
module ClassMethods
|
6
|
+
def perform(context = {})
|
7
|
+
action = new context
|
8
|
+
|
9
|
+
catch :_finish do
|
10
|
+
action.perform
|
11
|
+
end
|
12
|
+
|
13
|
+
action.context
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def context
|
18
|
+
@_context
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(context = Context.new)
|
22
|
+
@_context = if context.is_a? Context
|
23
|
+
context
|
24
|
+
else
|
25
|
+
Context.new context
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def cooperate(*actions)
|
30
|
+
actions.each do |action|
|
31
|
+
action.perform context
|
32
|
+
|
33
|
+
break if context.failure?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.prepended(base)
|
38
|
+
base.extend ClassMethods
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def success!
|
44
|
+
context.success!
|
45
|
+
throw :_finish
|
46
|
+
end
|
47
|
+
|
48
|
+
def failure!
|
49
|
+
context.failure!
|
50
|
+
throw :_finish
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_missing(method, *args, &block)
|
54
|
+
return context.send method, *args, &block if context.respond_to? method
|
55
|
+
|
56
|
+
super
|
57
|
+
end
|
58
|
+
end
|
data/spec/context.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'cooperator'
|
2
|
+
|
3
|
+
subject Cooperator::Context
|
4
|
+
|
5
|
+
spec 'dynamic setter and getter' do
|
6
|
+
context = Cooperator::Context.new
|
7
|
+
context.name = 'Apple'
|
8
|
+
|
9
|
+
assert context.name, :==, 'Apple'
|
10
|
+
end
|
11
|
+
|
12
|
+
spec '.new accepts a hash' do
|
13
|
+
context = Cooperator::Context.new name: 'Apple'
|
14
|
+
|
15
|
+
assert context.name, :==, 'Apple'
|
16
|
+
end
|
17
|
+
|
18
|
+
spec '#errors is an array' do
|
19
|
+
context = Cooperator::Context.new
|
20
|
+
|
21
|
+
assert context.errors, :is_a?, Array
|
22
|
+
end
|
23
|
+
|
24
|
+
spec '#success! marks the context as a success' do
|
25
|
+
context = Cooperator::Context.new
|
26
|
+
context.success!
|
27
|
+
|
28
|
+
assert context, :success?
|
29
|
+
refute context, :failure?
|
30
|
+
end
|
31
|
+
|
32
|
+
spec '#failure! marks the context as a failure' do
|
33
|
+
context = Cooperator::Context.new
|
34
|
+
context.failure!
|
35
|
+
|
36
|
+
assert context, :failure?
|
37
|
+
refute context, :success?
|
38
|
+
end
|
39
|
+
|
40
|
+
spec '#failure! accepts an error message' do
|
41
|
+
context = Cooperator::Context.new
|
42
|
+
context.failure! 'Failure!'
|
43
|
+
|
44
|
+
assert context.errors, :include?, 'Failure!'
|
45
|
+
end
|
46
|
+
|
47
|
+
spec '#include? returns true for an existing attribute' do
|
48
|
+
context = Cooperator::Context.new name: 'Apple'
|
49
|
+
|
50
|
+
assert context, :include?, :name
|
51
|
+
end
|
52
|
+
|
53
|
+
spec '#include? returns false for a non-existing attribute' do
|
54
|
+
context = Cooperator::Context.new
|
55
|
+
|
56
|
+
refute context, :include?, :name
|
57
|
+
end
|
data/spec/cooperate.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'cooperator'
|
2
|
+
|
3
|
+
class First
|
4
|
+
prepend Cooperator
|
5
|
+
|
6
|
+
def perform
|
7
|
+
context.first = 'first'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Second
|
12
|
+
prepend Cooperator
|
13
|
+
|
14
|
+
def perform
|
15
|
+
context.second = 'second'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Third
|
20
|
+
prepend Cooperator
|
21
|
+
|
22
|
+
def perform
|
23
|
+
context.third = 'third'
|
24
|
+
failure!
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Fourth
|
29
|
+
prepend Cooperator
|
30
|
+
|
31
|
+
def perform
|
32
|
+
context.fourth = 'fourth'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Success
|
37
|
+
prepend Cooperator
|
38
|
+
|
39
|
+
def perform
|
40
|
+
cooperate First,
|
41
|
+
Second
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Failure
|
46
|
+
prepend Cooperator
|
47
|
+
|
48
|
+
def perform
|
49
|
+
cooperate First,
|
50
|
+
Second,
|
51
|
+
Third,
|
52
|
+
Fourth
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
subject Cooperator
|
57
|
+
|
58
|
+
scope 'no failure' do
|
59
|
+
spec '#cooperate performs the given actions' do
|
60
|
+
context = Success.perform
|
61
|
+
|
62
|
+
assert context.first, :==, 'first'
|
63
|
+
assert context.second, :==, 'second'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
scope 'has failure' do
|
68
|
+
spec '#cooperate performs the given actions until a failure is met' do
|
69
|
+
context = Failure.perform
|
70
|
+
|
71
|
+
assert context.first, :==, 'first'
|
72
|
+
assert context.second, :==, 'second'
|
73
|
+
assert context.third, :==, 'third'
|
74
|
+
refute context, :include?, :fourth
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
data/spec/cooperator.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'cooperator'
|
2
|
+
|
3
|
+
class Interactor
|
4
|
+
prepend Cooperator
|
5
|
+
end
|
6
|
+
|
7
|
+
subject Cooperator
|
8
|
+
|
9
|
+
spec '#success? delegates to context.success?' do
|
10
|
+
interactor = Interactor.new
|
11
|
+
|
12
|
+
interactor.context.success!
|
13
|
+
|
14
|
+
assert interactor, :success?
|
15
|
+
|
16
|
+
interactor.context.failure!
|
17
|
+
|
18
|
+
refute interactor, :success?
|
19
|
+
end
|
20
|
+
|
21
|
+
spec '#failure? delegates to context.failure?' do
|
22
|
+
interactor = Interactor.new
|
23
|
+
|
24
|
+
interactor.context.failure!
|
25
|
+
|
26
|
+
assert interactor, :failure?
|
27
|
+
|
28
|
+
interactor.context.success!
|
29
|
+
|
30
|
+
refute interactor, :failure?
|
31
|
+
end
|
32
|
+
|
33
|
+
spec '#include? delegates to context.include?' do
|
34
|
+
interactor = Interactor.new
|
35
|
+
|
36
|
+
refute interactor, :include?, :name
|
37
|
+
|
38
|
+
interactor.context.name = 'Apple'
|
39
|
+
|
40
|
+
assert interactor, :include?, :name
|
41
|
+
end
|
42
|
+
|
43
|
+
spec 'delegate to the current context' do
|
44
|
+
interactor = Interactor.new name: 'Apple'
|
45
|
+
|
46
|
+
assert interactor.name, :==, 'Apple'
|
47
|
+
end
|
data/spec/failure.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'cooperator'
|
2
|
+
|
3
|
+
class Failure
|
4
|
+
prepend Cooperator
|
5
|
+
|
6
|
+
def perform
|
7
|
+
$before = true
|
8
|
+
failure!
|
9
|
+
$after = true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
prepare do
|
14
|
+
$before = false
|
15
|
+
$after = false
|
16
|
+
end
|
17
|
+
|
18
|
+
subject Cooperator
|
19
|
+
|
20
|
+
spec '.perform runs until #failure! is called' do
|
21
|
+
Failure.perform
|
22
|
+
|
23
|
+
assert $before
|
24
|
+
refute $after
|
25
|
+
end
|
26
|
+
|
27
|
+
spec '.perform returns a failure context' do
|
28
|
+
context = Failure.perform
|
29
|
+
|
30
|
+
assert context, :failure?
|
31
|
+
refute context, :success?
|
32
|
+
end
|
data/spec/success.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'cooperator'
|
2
|
+
|
3
|
+
class ImplicitSuccess
|
4
|
+
prepend Cooperator
|
5
|
+
|
6
|
+
def perform
|
7
|
+
$before = true
|
8
|
+
$after = true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ExplicitSuccess
|
13
|
+
prepend Cooperator
|
14
|
+
|
15
|
+
def perform
|
16
|
+
$before = true
|
17
|
+
success!
|
18
|
+
$after = true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
prepare do
|
23
|
+
$before = false
|
24
|
+
$after = false
|
25
|
+
end
|
26
|
+
|
27
|
+
subject Cooperator
|
28
|
+
|
29
|
+
scope 'implicit success' do
|
30
|
+
spec '.perform runs until #success! is called' do
|
31
|
+
ImplicitSuccess.perform
|
32
|
+
|
33
|
+
assert $before
|
34
|
+
assert $after
|
35
|
+
end
|
36
|
+
|
37
|
+
spec '.perform returns a success context' do
|
38
|
+
context = ImplicitSuccess.perform
|
39
|
+
|
40
|
+
assert context, :success?
|
41
|
+
refute context, :failure?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
scope 'explicit success' do
|
46
|
+
spec '.perform runs until #success! is called' do
|
47
|
+
ExplicitSuccess.perform
|
48
|
+
|
49
|
+
assert $before
|
50
|
+
refute $after
|
51
|
+
end
|
52
|
+
|
53
|
+
spec '.perform returns a success context' do
|
54
|
+
context = ExplicitSuccess.perform
|
55
|
+
|
56
|
+
assert context, :success?
|
57
|
+
refute context, :failure?
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cooperator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Erol Fornoles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-07 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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
|
+
description: Simple cooperative interactors for Ruby
|
42
|
+
email:
|
43
|
+
- erol.fornoles@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- cooperator.gemspec
|
54
|
+
- lib/cooperator.rb
|
55
|
+
- lib/cooperator/context.rb
|
56
|
+
- lib/cooperator/version.rb
|
57
|
+
- spec/context.rb
|
58
|
+
- spec/cooperate.rb
|
59
|
+
- spec/cooperator.rb
|
60
|
+
- spec/failure.rb
|
61
|
+
- spec/success.rb
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.2.0
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Simple cooperative interactors for Ruby
|
86
|
+
test_files:
|
87
|
+
- spec/context.rb
|
88
|
+
- spec/cooperate.rb
|
89
|
+
- spec/cooperator.rb
|
90
|
+
- spec/failure.rb
|
91
|
+
- spec/success.rb
|