single_action_service 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccec320aef581ff48f0c28df5af6aae1bf230d9e89a9f5d57a3f8d24198af9df
4
- data.tar.gz: 65c81e05d2e24048d68108759a69a173d4ed98c3f6a53122ee84da932006368d
3
+ metadata.gz: a4d4eb3fec3dc55edbf5aab8460812be72f6ff3f088a75a530b42a00ad904b1c
4
+ data.tar.gz: a1f12498a8534810f69ba23df56ceff9cc3d3f2ea3cbea91a0429d61bac70551
5
5
  SHA512:
6
- metadata.gz: 6ad02a79662b3ebf898b9a39e2fd3964a857a555625911585269ab828223567ec276f4494207304de91bd975ec90c3d6d9bc6ef19007cd1539922b730a85323a
7
- data.tar.gz: 16e68e8501718248f59f044edbd096feb8efce0dbe42ba42169d6987e9318250d12e3523fdf1dbdd6e203cc7cc571a5202382644989469efc92761df9c2a47ae
6
+ metadata.gz: 3c192083a4ffba6795f46eb684b9f7e1b3b6805f0c1a87d889c78a64c73aba4c4bed6e9a1daa4fb84a9bfd97a5160dabe3d5b982a43a3f0f656f40d8299f24c1
7
+ data.tar.gz: ceced5347723cc62ea5f4fe30b8721a6e295ebc8163d5c075cdc424f3b9b1b321d79ba00e64ceace8634f6d60732e7536ec3dab477989bb5f0cbba0390240b01
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # SingleActionService
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/single_action_service`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/single_action_service.svg)](https://rubygems.org/gems/single_action_service)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Implementation of a Service Object pattern in Ruby.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,22 +22,74 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
25
+ 1. Inherit:
26
+ ```ruby
27
+ class MySingleAction < SingleActionService::Base
28
+ end
29
+ ```
30
+ 2. Create a constructor with parameters or pass them to call:
31
+ ```ruby
32
+ # Usually a data source is passed
33
+ def initialize(x, y)
34
+ @x = x
35
+ @y = y
36
+ end
37
+ ```
38
+ or
39
+ ```ruby
40
+ def call(x, y)
41
+ end
42
+ ```
43
+ 3. Perform the action:
44
+ ```ruby
45
+ sum = x + y
46
+ ```
47
+ 4. Return the result:
48
+ ```ruby
49
+ success(sum)
50
+ ```
51
+ or without data
52
+ ```ruby
53
+ success
54
+ ```
55
+ 5. Or return an error:
56
+ ```ruby
57
+ return error(sum, code: 1) unless sum.positive?
58
+ ```
59
+ The first parameter is any data (optional).
28
60
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+ The 'code' parameter is used to identify the error (optional).
30
62
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+ 6. You can process the result received from the service as follows:
64
+ ```ruby
65
+ action = MySingleAction.new
66
+ result = action.call(1,2)
67
+ # or uses result.error?
68
+ if result.success?
69
+ result.data
70
+ else
71
+ {
72
+ error_code: result.error_code,
73
+ data: result.data
74
+ }
75
+ end
76
+ ```
77
+ or uses data! if you want to throw an exception
78
+ ```ruby
79
+ begin
80
+ result = MySingleAction.new.call(1,2).data!
81
+ rescue SingleActionService::InvalidResult => e
82
+ {
83
+ error_code: e.result.error_code,
84
+ data: e.result.data
85
+ }
86
+ end
87
+ ```
32
88
 
33
89
  ## Contributing
34
90
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/single_action_service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sequenia/single_action_service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
92
 
37
93
  ## License
38
94
 
39
95
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the SingleActionService project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/single_action_service/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SingleActionService
4
+ class Error < StandardError; end
5
+
6
+ class InvalidResult < ArgumentError
7
+ attr_reader :result
8
+
9
+ def initialize(result)
10
+ @result = result
11
+ end
12
+ end
13
+ end
@@ -15,5 +15,11 @@ module SingleActionService
15
15
  def error?
16
16
  !success?
17
17
  end
18
+
19
+ def data!
20
+ return data if success?
21
+
22
+ raise InvalidResult.new(self)
23
+ end
18
24
  end
19
25
  end
@@ -1,3 +1,3 @@
1
1
  module SingleActionService
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,7 +1,7 @@
1
+ require "single_action_service/exceptions"
1
2
  require "single_action_service/base"
2
3
  require "single_action_service/result"
3
4
  require "single_action_service/version"
4
5
 
5
6
  module SingleActionService
6
- class Error < StandardError; end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: single_action_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bazov Peter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-24 00:00:00.000000000 Z
11
+ date: 2020-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,7 @@ files:
72
72
  - bin/setup
73
73
  - lib/single_action_service.rb
74
74
  - lib/single_action_service/base.rb
75
+ - lib/single_action_service/exceptions.rb
75
76
  - lib/single_action_service/result.rb
76
77
  - lib/single_action_service/version.rb
77
78
  - single_action_service.gemspec
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  - !ruby/object:Gem::Version
95
96
  version: '0'
96
97
  requirements: []
97
- rubygems_version: 3.0.3
98
+ rubygems_version: 3.0.4
98
99
  signing_key:
99
100
  specification_version: 4
100
101
  summary: Single Action Service