light-service 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/light-service/context.rb +15 -0
- data/lib/light-service/version.rb +1 -1
- data/spec/acceptance/add_numbers_spec.rb +10 -9
- data/spec/action_spec.rb +2 -2
- data/spec/context_spec.rb +41 -82
- data/spec/organizer_spec.rb +9 -15
- data/spec/sample/tax/looks_up_tax_percentage_action.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6e076aae96c52a5762908422611819327dde2bc
|
4
|
+
data.tar.gz: 97f23597368c33f4e5fca9d829b286ae73db14d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6aeba0dd3f98960c6e5b13a878018afb5e7f2c6a50b0254dfcadf13b6045c649df51f29c07fd8b023c4f3d075551a6923d63919f3870c2cebc3ce2bf05067ed
|
7
|
+
data.tar.gz: f9cf3c1bff5df5411bee9240de31192d8a63f42175335905fd5c62d5e1b11d99daf7d824cdd1db8f69348982d17cda18a9a6db77fc3b2d3bbab9f5c73a694a09
|
data/README.md
CHANGED
@@ -97,7 +97,7 @@ class LooksUpTaxPercentageAction
|
|
97
97
|
|
98
98
|
def self.object_is_nil?(object, context, message)
|
99
99
|
if object.nil?
|
100
|
-
context.
|
100
|
+
context.fail!(message)
|
101
101
|
return true
|
102
102
|
end
|
103
103
|
|
@@ -189,9 +189,9 @@ Huge thanks to the [contributors](https://github.com/adomokos/light-service/grap
|
|
189
189
|
|
190
190
|
## Release Notes
|
191
191
|
|
192
|
-
### 0.0
|
193
|
-
*
|
194
|
-
*
|
192
|
+
### 0.2.0
|
193
|
+
* [Renaming](https://github.com/adomokos/light-service/commit/8d40ff7d393a157a8a558f9e4e021b8731550834) the `set_success!` and `set_failure!` methods to `succeed!` and `fail!`.
|
194
|
+
* [Throwing](https://github.com/adomokos/light-service/commit/5ef315b8aeeafc99e38676adad3c11df5d93b0e3) an ArgumentError if the `make` method's argument is not Hash or LightService::Context.
|
195
195
|
|
196
196
|
## License
|
197
197
|
|
@@ -14,6 +14,10 @@ module LightService
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.make(context={})
|
17
|
+
unless context.is_a? Hash or context.is_a? ::LightService::Context
|
18
|
+
raise ArgumentError, 'Argument must be Hash or LightService::Context'
|
19
|
+
end
|
20
|
+
|
17
21
|
return context if context.is_a?(Context)
|
18
22
|
self.new(context, ::LightService::Outcomes::SUCCESS, '')
|
19
23
|
end
|
@@ -42,11 +46,21 @@ module LightService
|
|
42
46
|
end
|
43
47
|
|
44
48
|
def set_success!(message)
|
49
|
+
warn 'DEPRECATED: Please use `succeed!` instead'
|
50
|
+
succeed!(message)
|
51
|
+
end
|
52
|
+
|
53
|
+
def succeed!(message)
|
45
54
|
@message = message
|
46
55
|
@outcome = ::LightService::Outcomes::SUCCESS
|
47
56
|
end
|
48
57
|
|
49
58
|
def set_failure!(message)
|
59
|
+
warn 'DEPRECATED: Please use `fail!` instead'
|
60
|
+
fail!(message)
|
61
|
+
end
|
62
|
+
|
63
|
+
def fail!(message)
|
50
64
|
@message = message
|
51
65
|
@outcome = ::LightService::Outcomes::FAILURE
|
52
66
|
end
|
@@ -55,5 +69,6 @@ module LightService
|
|
55
69
|
@message = message
|
56
70
|
@skip_all = true
|
57
71
|
end
|
72
|
+
|
58
73
|
end
|
59
74
|
end
|
@@ -3,13 +3,12 @@ require 'spec_helper'
|
|
3
3
|
class Organizer
|
4
4
|
include LightService::Organizer
|
5
5
|
|
6
|
-
def self.
|
7
|
-
with(number
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
]
|
6
|
+
def self.add_numbers(number)
|
7
|
+
with(:number => number).reduce(
|
8
|
+
AddsOneAction,
|
9
|
+
AddsTwoAction,
|
10
|
+
AddsThreeAction
|
11
|
+
)
|
13
12
|
end
|
14
13
|
end
|
15
14
|
|
@@ -48,7 +47,9 @@ end
|
|
48
47
|
|
49
48
|
describe Organizer do
|
50
49
|
it "Adds 1 2 3 and through to 1" do
|
51
|
-
result = Organizer.
|
52
|
-
result.fetch(:number)
|
50
|
+
result = Organizer.add_numbers 1
|
51
|
+
number = result.fetch(:number)
|
52
|
+
|
53
|
+
expect(number).to eq(7)
|
53
54
|
end
|
54
55
|
end
|
data/spec/action_spec.rb
CHANGED
@@ -18,11 +18,11 @@ module LightService
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
let(:context) { ::LightService::Context.
|
21
|
+
let(:context) { ::LightService::Context.make }
|
22
22
|
|
23
23
|
context "when the action context has failure" do
|
24
24
|
it "returns immediately" do
|
25
|
-
context.
|
25
|
+
context.fail!("an error")
|
26
26
|
|
27
27
|
DummyAction.execute(context)
|
28
28
|
|
data/spec/context_spec.rb
CHANGED
@@ -2,109 +2,68 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module LightService
|
4
4
|
describe Context do
|
5
|
-
subject { Context.new({:test => 1}, Outcomes::SUCCESS, "some_message") }
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
expect(service_result[:test]).to eq 1
|
13
|
-
end
|
14
|
-
|
15
|
-
it "initializes the object with the context" do
|
16
|
-
service_result = Context.new.tap { |o| o.add_to_context({test: 1})}
|
17
|
-
|
18
|
-
expect(service_result).to be_success
|
19
|
-
expect(service_result.message).to eq ''
|
20
|
-
expect(service_result[:test]).to eq 1
|
21
|
-
end
|
22
|
-
|
23
|
-
describe ".make" do
|
24
|
-
it "initializes the object with make" do
|
25
|
-
service_result = Context.make({test: 1})
|
26
|
-
|
27
|
-
expect(service_result).to be_success
|
28
|
-
expect(service_result.message).to eq ""
|
29
|
-
expect(service_result[:test]).to eq 1
|
30
|
-
end
|
31
|
-
|
32
|
-
context "when passing valid parameters" do
|
33
|
-
subject { Context.make(params).to_hash }
|
34
|
-
|
35
|
-
let(:params) { {test: 1} }
|
36
|
-
it { should eq({test: 1}) }
|
37
|
-
|
38
|
-
let(:params) { Context.make(test: 1) }
|
39
|
-
it { should eq({test: 1}) }
|
6
|
+
describe "can be made" do
|
7
|
+
context "with no arguments" do
|
8
|
+
subject { Context.make }
|
9
|
+
it { should be_success }
|
10
|
+
its(:message) { should be_empty }
|
40
11
|
end
|
41
12
|
|
42
|
-
context "
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
let(:invalid_params) { [] }
|
49
|
-
it { should raise_error(NoMethodError) }
|
13
|
+
context "with a hash" do
|
14
|
+
it "has the hash values" do
|
15
|
+
context = Context.make(:one => 1)
|
16
|
+
expect(context[:one]).to eq(1)
|
17
|
+
end
|
50
18
|
end
|
51
19
|
|
52
|
-
context "
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
new_context = Context.make(original_context)
|
57
|
-
expect(new_context.object_id).to eq(original_context.object_id)
|
20
|
+
context "with FAILURE" do
|
21
|
+
it "is failed" do
|
22
|
+
context = Context.new({}, ::LightService::Outcomes::FAILURE, '')
|
23
|
+
expect(context).to be_failure
|
58
24
|
end
|
59
25
|
end
|
60
26
|
end
|
61
27
|
|
62
|
-
describe "
|
63
|
-
|
64
|
-
Context.make(
|
65
|
-
Context.make({}).to_hash.should == {}
|
28
|
+
describe "can't be made" do
|
29
|
+
specify "with invalid parameters" do
|
30
|
+
expect{Context.make([])}.to raise_error(ArgumentError)
|
66
31
|
end
|
67
32
|
end
|
68
33
|
|
69
|
-
|
70
|
-
|
34
|
+
it "can be asked for success?" do
|
35
|
+
context = Context.new({}, ::LightService::Outcomes::SUCCESS)
|
36
|
+
expect(context.success?).to be_true
|
71
37
|
end
|
72
38
|
|
73
|
-
it "
|
74
|
-
|
75
|
-
|
76
|
-
expect(subject).to be_success
|
77
|
-
expect(subject).not_to be_failure
|
78
|
-
expect(subject.message) == 'the success'
|
39
|
+
it "can be asked for failure?" do
|
40
|
+
context = Context.new({}, ::LightService::Outcomes::FAILURE)
|
41
|
+
expect(context.failure?).to be_true
|
79
42
|
end
|
80
43
|
|
81
|
-
it "
|
82
|
-
|
83
|
-
|
84
|
-
expect(
|
85
|
-
expect(subject).to be_failure
|
86
|
-
expect(subject.message).to eq('the failure')
|
44
|
+
it "can be asked for skip_all?" do
|
45
|
+
context = Context.make
|
46
|
+
context.skip_all!
|
47
|
+
expect(context.skip_all?).to be_true
|
87
48
|
end
|
88
49
|
|
89
|
-
it "
|
90
|
-
|
91
|
-
|
92
|
-
expect(
|
93
|
-
expect(subject).to be_success
|
94
|
-
expect(subject).not_to be_failure
|
95
|
-
expect(subject.message).to eq('the reason to skip')
|
50
|
+
it "can be pushed into a SUCCESS state" do
|
51
|
+
context = Context.make
|
52
|
+
context.succeed!("a happy end")
|
53
|
+
expect(context).to be_success
|
96
54
|
end
|
97
55
|
|
98
|
-
it "
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
56
|
+
it "can be pushed into a FAILURE state" do
|
57
|
+
context = Context.make
|
58
|
+
context.fail!("a sad end")
|
59
|
+
expect(context).to be_failure
|
60
|
+
end
|
103
61
|
|
104
|
-
|
105
|
-
|
106
|
-
|
62
|
+
it "can set a flag to skip all subsequent actions" do
|
63
|
+
context = Context.make
|
64
|
+
context.skip_all!
|
65
|
+
expect(context).to be_skip_all
|
107
66
|
end
|
108
|
-
end
|
109
67
|
|
68
|
+
end
|
110
69
|
end
|
data/spec/organizer_spec.rb
CHANGED
@@ -7,18 +7,12 @@ describe LightService::Organizer do
|
|
7
7
|
class AnOrganizer
|
8
8
|
include LightService::Organizer
|
9
9
|
|
10
|
-
def self.
|
11
|
-
with(
|
10
|
+
def self.do_something(action_arguments)
|
11
|
+
with(action_arguments).reduce([AnAction, AnotherAction])
|
12
12
|
end
|
13
13
|
|
14
|
-
def self.
|
15
|
-
|
16
|
-
with(context).reduce(AnAction, AnotherAction)
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.some_method_with_no_actions(user)
|
20
|
-
context = LightService::Context.make(user: user)
|
21
|
-
with(context).reduce
|
14
|
+
def self.do_something_with_no_actions(action_arguments)
|
15
|
+
with(action_arguments).reduce
|
22
16
|
end
|
23
17
|
end
|
24
18
|
|
@@ -35,8 +29,8 @@ describe LightService::Organizer do
|
|
35
29
|
.and_return context
|
36
30
|
end
|
37
31
|
|
38
|
-
it "creates a Context" do
|
39
|
-
result = AnOrganizer.
|
32
|
+
it "implicitly creates a Context" do
|
33
|
+
result = AnOrganizer.do_something(:user => user)
|
40
34
|
expect(result).to eq(context)
|
41
35
|
end
|
42
36
|
end
|
@@ -51,15 +45,15 @@ describe LightService::Organizer do
|
|
51
45
|
.and_return context
|
52
46
|
end
|
53
47
|
|
54
|
-
it "
|
55
|
-
result = AnOrganizer.
|
48
|
+
it "uses that Context without recreating it" do
|
49
|
+
result = AnOrganizer.do_something(context)
|
56
50
|
expect(result).to eq(context)
|
57
51
|
end
|
58
52
|
end
|
59
53
|
|
60
54
|
context "when no Actions are specified" do
|
61
55
|
it "throws a Runtime error" do
|
62
|
-
expect { AnOrganizer.
|
56
|
+
expect { AnOrganizer.do_something_with_no_actions(context) }.to \
|
63
57
|
raise_error RuntimeError, "No action(s) were provided"
|
64
58
|
end
|
65
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: light-service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Attila Domokos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|