servizio 0.2.0 → 0.3.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: 813806f66607dba01c96b178f0ccde49ff28e845
4
- data.tar.gz: 30fba7afa7b7d808ceb021b84e490215b698ad4a
3
+ metadata.gz: e88521b5ae99b6b45c50f81766f39dfeb2223bd7
4
+ data.tar.gz: a0e981be9e716218e3e1598a8288dcb8829670ea
5
5
  SHA512:
6
- metadata.gz: 3cf60f0f2d1fa2fb171ba95c40a1776e8eb4413d760e50895cfc9edf254009682c519950f43a43c799b981a2232af9aa555dc15cbe1859a77c660327605f177a
7
- data.tar.gz: e77581b8dc72c5d940b21221972192e799c84bae43c996b4cbf44bf712b453e04ca997b8d5d39c97aef50f432b5d90dcbfba403293f7ca91c10b5d157f75fdb9
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
- gem "codeclimate-test-reporter", group: :test, require: nil
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
- gem "pry", "~> 0.9.12.6"
9
- gem 'pry-byebug', "<= 1.3.2"
10
- gem "pry-stack_explorer", "~> 0.4.9.1"
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
  [![Build Status](https://travis-ci.org/msievers/servizio.svg?branch=master)](https://travis-ci.org/msievers/servizio)
4
4
  [![Test Coverage](https://codeclimate.com/github/msievers/servizio/badges/coverage.svg)](https://codeclimate.com/github/msievers/servizio)
5
5
  [![Code Climate](https://codeclimate.com/github/msievers/servizio/badges/gpa.svg)](https://codeclimate.com/github/msievers/servizio)
6
+ [![Dependency Status](https://gemnasium.com/msievers/servizio.svg)](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.result # => 6
31
+ operation.call!.result # => 6
31
32
  ```
32
33
 
33
34
  ## Additional readings
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Servizio
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,26 +1,44 @@
1
1
  describe Servizio::Service do
2
- context "if derived" do
3
- let(:service) do
4
- Class.new(described_class) do
5
- attr_accessor :should_fail
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
- validates_presence_of :summands
7
+ validates_presence_of :summands
9
8
 
10
- def should_fail?
11
- @should_fail == true
12
- end
9
+ def should_fail?
10
+ @should_fail == true
11
+ end
13
12
 
14
- def call
15
- if should_fail?
16
- errors.add(:call, "failed")
17
- else
18
- summands.reduce(:+)
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.2.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-04 00:00:00.000000000 Z
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel