servizio 0.2.0 → 0.3.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/Gemfile +12 -5
- data/README.md +2 -1
- data/lib/servizio/service.rb +17 -0
- data/lib/servizio/version.rb +1 -1
- data/spec/servizio/service_spec.rb +33 -15
- 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: e88521b5ae99b6b45c50f81766f39dfeb2223bd7
|
4
|
+
data.tar.gz: a0e981be9e716218e3e1598a8288dcb8829670ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b880b1b89e6dc594ffa754c123819e7d46f3e328907986e9ef147f3e72ee2cf5ff2c1bca9b9cd1b1316ce8a8574249a12e3e9a8eaacb083e388af4d2bee02f48
|
7
|
+
data.tar.gz: d3e740be6edff8b103fcd9a1427549a11152b0915f82359b213ee5d5f064fd3d2c4672df8bd5cda4c68c688b82421909ddc5fd3dbe4b4863c23427a0b7a01c9b
|
data/Gemfile
CHANGED
@@ -3,9 +3,16 @@ source "https://rubygems.org"
|
|
3
3
|
# Specify your gem's dependencies in servizio.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
|
6
|
+
if !ENV["CI"]
|
7
|
+
group :development do
|
8
|
+
gem "pry", "~> 0.9.12.6"
|
9
|
+
gem "pry-byebug", "<= 1.3.2"
|
10
|
+
gem "pry-rescue", "~> 1.4.1"
|
11
|
+
gem "pry-stack_explorer", "~> 0.4.9.1"
|
12
|
+
gem "pry-syntax-hacks", "~> 0.0.6"
|
13
|
+
end
|
14
|
+
end
|
7
15
|
|
8
|
-
|
9
|
-
gem
|
10
|
-
|
11
|
-
gem "pry-syntax-hacks", "~> 0.0.6"
|
16
|
+
group :test do
|
17
|
+
gem "codeclimate-test-reporter", require: nil
|
18
|
+
end
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[](https://travis-ci.org/msievers/servizio)
|
4
4
|
[](https://codeclimate.com/github/msievers/servizio)
|
5
5
|
[](https://codeclimate.com/github/msievers/servizio)
|
6
|
+
[](https://gemnasium.com/msievers/servizio)
|
6
7
|
|
7
8
|
Servizio is a gem to support you creating service objects. It was created after I read a blog post about [service objects](http://brewhouse.io/blog/2014/04/30/gourmet-service-objects.html) from [Philippe Creux](https://twitter.com/pcreux). Realy great post, check it out.
|
8
9
|
|
@@ -27,7 +28,7 @@ end
|
|
27
28
|
operation = MyService.new(operands: [1,2,3])
|
28
29
|
|
29
30
|
# call the operation and get it's result
|
30
|
-
operation.call
|
31
|
+
operation.call!.result # => 6
|
31
32
|
```
|
32
33
|
|
33
34
|
## Additional readings
|
data/lib/servizio/service.rb
CHANGED
@@ -6,6 +6,8 @@ class Servizio::Service
|
|
6
6
|
|
7
7
|
attr_accessor :result
|
8
8
|
|
9
|
+
OperationFailedError = Class.new(StandardError)
|
10
|
+
OperationInvalidError = Class.new(StandardError)
|
9
11
|
OperationNotCalledError = Class.new(StandardError)
|
10
12
|
|
11
13
|
# http://stackoverflow.com/questions/14431723/activemodelvalidations-on-anonymous-class
|
@@ -13,6 +15,21 @@ class Servizio::Service
|
|
13
15
|
super ? super : "__anonymous_servizio_service_class__"
|
14
16
|
end
|
15
17
|
|
18
|
+
# shortcut to call operations, which returns the result and/or throws errors
|
19
|
+
def self.call(*args)
|
20
|
+
operation = self.new(*args)
|
21
|
+
|
22
|
+
if operation.valid?
|
23
|
+
if operation.call!.succeeded?
|
24
|
+
operation.result
|
25
|
+
else
|
26
|
+
raise Servizio::Service::OperationFailedError
|
27
|
+
end
|
28
|
+
else
|
29
|
+
raise Servizio::Service::OperationInvalidError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
16
33
|
def result
|
17
34
|
called? ? @result : (raise OperationNotCalledError)
|
18
35
|
end
|
data/lib/servizio/version.rb
CHANGED
@@ -1,26 +1,44 @@
|
|
1
1
|
describe Servizio::Service do
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
attr_accessor :summands
|
2
|
+
let(:service) do
|
3
|
+
Class.new(described_class) do
|
4
|
+
attr_accessor :should_fail
|
5
|
+
attr_accessor :summands
|
7
6
|
|
8
|
-
|
7
|
+
validates_presence_of :summands
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def should_fail?
|
10
|
+
@should_fail == true
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
13
|
+
def call
|
14
|
+
if should_fail?
|
15
|
+
errors.add(:call, "failed")
|
16
|
+
else
|
17
|
+
summands.reduce(:+)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".call" do
|
24
|
+
it "creates an instance of the service, calls it with the given arguments and returns the result" do
|
25
|
+
expect(service.call(summands: [1,2])).to eq(3)
|
26
|
+
end
|
27
|
+
|
28
|
+
context "if the operation is invalid" do
|
29
|
+
it "raises an error" do
|
30
|
+
expect { service.call }.to raise_error(Servizio::Service::OperationInvalidError)
|
31
|
+
end
|
32
|
+
end
|
23
33
|
|
34
|
+
context "if the operation did not succeeded" do
|
35
|
+
it "raises an error" do
|
36
|
+
expect { service.call(summands: [1,2], should_fail: true) }.to raise_error(Servizio::Service::OperationFailedError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "if derived" do
|
24
42
|
let(:summands) { [1,2,3] }
|
25
43
|
let(:succeeding_operation) { service.new summands: summands }
|
26
44
|
let(:failing_operation) { service.new summands: summands, should_fail: true }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: servizio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Sievers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|