cooperator 0.1.2 → 0.2.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: 92514ccba0d6cd8eba54cfb1ed6b674da68b0e1d
4
- data.tar.gz: 5e36f0a442b5e6e67e452994a2dbadb9388ca542
3
+ metadata.gz: 3d6944ab9d458de00ea6f81e9b1f213dd7ad56f7
4
+ data.tar.gz: 7d1f04792af4096867c25e8bf64211655db50955
5
5
  SHA512:
6
- metadata.gz: 9ec7b8b5db064b39a54ee47dee4751c586f7592ba6d14abd23f2294219bd85aa31f5744af01a9c616addabc4e3a7ac06db6299e794ac3d315c671042054ecbb0
7
- data.tar.gz: 41e688708acd72c321679e3c509f0f8c0067ce9d0b94be2359289c016d4412594378725e2ca52e9935465897fadce5b635500c10981cb72b072dab2f857ab327
6
+ metadata.gz: 53bffa767b3a518725020964b50036b17ad847ef1947f9681c9582f8edc286fd615700e02c4fd044cd78ac412f2db74c76b897036e495cf377f0e969fbee69ac
7
+ data.tar.gz: 2ed544a93ca8b55abfc04000c1041a2d882e8a17ae727f89ad2ee8707ad3fd95c0647ec676d3deecc54ff21874a49f6d66e07cdf96ab004ec8c47faec0553717
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Code Climate](https://codeclimate.com/github/Erol/cooperator.png)](https://codeclimate.com/github/Erol/cooperator)
1
+ [![Code Climate Score](http://img.shields.io/codeclimate/github/Erol/cooperator.svg?style=flat)](https://codeclimate.com/github/Erol/cooperator)
2
2
 
3
3
  # Cooperator
4
4
 
@@ -1,3 +1,3 @@
1
1
  module Cooperator
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/cooperator.rb CHANGED
@@ -3,13 +3,63 @@ require 'cooperator/context'
3
3
 
4
4
  module Cooperator
5
5
  module ClassMethods
6
+ def expected
7
+ @_expected ||= []
8
+ end
9
+
10
+ def accepted
11
+ @_accepted ||= []
12
+ end
13
+
14
+ def committed
15
+ @_committed ||= []
16
+ end
17
+
18
+ def expects(*properties)
19
+ properties.each do |property|
20
+ define_method property do
21
+ context.send property
22
+ end
23
+
24
+ expected << property
25
+ end
26
+ end
27
+
28
+ def accepts(*properties)
29
+ properties.each do |property|
30
+ define_method property do
31
+ if context.include? property
32
+ context.send property
33
+ else
34
+ nil
35
+ end
36
+ end
37
+
38
+ accepted << property
39
+ end
40
+ end
41
+
42
+ def commits(*properties)
43
+ properties.each do |property|
44
+ committed << property
45
+ end
46
+ end
47
+
6
48
  def perform(context = {})
49
+ expected.each do |property|
50
+ raise Exception, "missing expected property: #{expect}" unless context.include? expect
51
+ end
52
+
7
53
  action = new context
8
54
 
9
55
  catch :_finish do
10
56
  action.perform
11
57
  end
12
58
 
59
+ committed.each do |property|
60
+ raise Exception, "missing committed property: #{expect}" unless context.include? expect
61
+ end
62
+
13
63
  action.context
14
64
  end
15
65
  end
@@ -45,14 +95,20 @@ module Cooperator
45
95
  throw :_finish
46
96
  end
47
97
 
98
+ def success?
99
+ context.success?
100
+ end
101
+
48
102
  def failure!(messages = {})
49
103
  context.failure! messages
50
104
  throw :_finish
51
105
  end
52
106
 
53
- def method_missing(method, *args, &block)
54
- return context.send method, *args, &block if context.respond_to? method
107
+ def failure?
108
+ context.failure?
109
+ end
55
110
 
56
- super
111
+ def include?(property)
112
+ context.include?(property)
57
113
  end
58
114
  end
data/spec/failure.rb CHANGED
@@ -10,6 +10,14 @@ class Failure
10
10
  end
11
11
  end
12
12
 
13
+ class FailureWithErrors
14
+ prepend Cooperator
15
+
16
+ def perform
17
+ failure! action: 'Failure!'
18
+ end
19
+ end
20
+
13
21
  prepare do
14
22
  $before = false
15
23
  $after = false
@@ -30,3 +38,9 @@ spec '.perform returns a failure context' do
30
38
  assert context, :failure?
31
39
  refute context, :success?
32
40
  end
41
+
42
+ spec '.perform returns a failure with errors context' do
43
+ context = FailureWithErrors.perform
44
+
45
+ assert context.errors, :==, action: ['Failure!']
46
+ end
data/spec/perform.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'cooperator'
2
+
3
+ class Interactor
4
+ prepend Cooperator
5
+
6
+ expects :input
7
+ commits :output
8
+ end
9
+
10
+ scope '.perform' do
11
+ spec 'raises an exception when an expected input is missing' do
12
+ raises Exception do
13
+ Interactor.perform
14
+ end
15
+ end
16
+
17
+ spec 'raises an exception when a committed output is missing' do
18
+ raises Exception do
19
+ Interactor.perform
20
+ end
21
+ end
22
+ end
@@ -39,18 +39,3 @@ spec '#include? delegates to context.include?' do
39
39
 
40
40
  assert interactor, :include?, :name
41
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
48
-
49
- spec '#failure! accepts an error key and message' do
50
- context = Cooperator::Context.new
51
- interactor = Interactor.new context
52
-
53
- interactor.failure! action: 'Failure!'
54
-
55
- assert context.errors, :==, action: ['Failure!']
56
- end
@@ -0,0 +1,58 @@
1
+ require 'cooperator'
2
+
3
+ class Interactor
4
+ prepend Cooperator
5
+
6
+ expects :apple, :banana
7
+ accepts :coconut, :durian
8
+
9
+ commits :fig
10
+
11
+ def perform
12
+ value = apple
13
+ value = banana
14
+ value = coconut
15
+ value = durian
16
+ end
17
+ end
18
+
19
+ scope '.expects' do
20
+ spec 'adds given properties as expected/required input' do
21
+ assert Interactor.expected, :include?, :apple
22
+ assert Interactor.expected, :include?, :banana
23
+ end
24
+
25
+ spec 'delegate given properties to the current context' do
26
+ interactor = Interactor.new apple: 'Apple', banana: 'Banana'
27
+
28
+ assert interactor.apple, :==, 'Apple'
29
+ assert interactor.banana, :==, 'Banana'
30
+ end
31
+ end
32
+
33
+ scope '.accepts' do
34
+ spec 'adds given properties as accepted/optional input' do
35
+ assert Interactor.accepted, :include?, :coconut
36
+ assert Interactor.accepted, :include?, :durian
37
+ end
38
+
39
+ spec 'delegate given properties to the current context' do
40
+ interactor = Interactor.new coconut: 'Coconut', durian: 'Durian'
41
+
42
+ assert interactor.coconut, :==, 'Coconut'
43
+ assert interactor.durian, :==, 'Durian'
44
+ end
45
+
46
+ spec 'return nil for properties not included in the current context' do
47
+ interactor = Interactor.new
48
+
49
+ assert interactor.coconut, :==, nil
50
+ assert interactor.durian, :==, nil
51
+ end
52
+ end
53
+
54
+ scope '.commits' do
55
+ spec 'adds given properties as committed/required output' do
56
+ assert Interactor.committed, :include?, :fig
57
+ end
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cooperator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erol Fornoles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-11 00:00:00.000000000 Z
11
+ date: 2014-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,8 +56,10 @@ files:
56
56
  - lib/cooperator/version.rb
57
57
  - spec/context.rb
58
58
  - spec/cooperate.rb
59
- - spec/cooperator.rb
60
59
  - spec/failure.rb
60
+ - spec/perform.rb
61
+ - spec/predicates.rb
62
+ - spec/properties.rb
61
63
  - spec/success.rb
62
64
  homepage: ''
63
65
  licenses:
@@ -86,6 +88,8 @@ summary: Simple cooperative interactors for Ruby
86
88
  test_files:
87
89
  - spec/context.rb
88
90
  - spec/cooperate.rb
89
- - spec/cooperator.rb
90
91
  - spec/failure.rb
92
+ - spec/perform.rb
93
+ - spec/predicates.rb
94
+ - spec/properties.rb
91
95
  - spec/success.rb