good_services 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +136 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/good_services.gemspec +26 -0
- data/lib/good_services/base.rb +76 -0
- data/lib/good_services/version.rb +3 -0
- data/lib/good_services.rb +5 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab9aea86bdfee7c671ed7b0b880ca45f2599cf1b
|
4
|
+
data.tar.gz: edb3d4af284f44bec355cd3d6e0e577716c015a1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 499c9fca7ab70df466f3ddf2bfa03156fd3c4f3bc4c630cfdf759b0bf1f00d86313940ffcfb51a60319addd6d5e39a6d7e08c881837f23653b40628c6d11998b
|
7
|
+
data.tar.gz: fc6c806f50865d71f41720184ed440abecedfb7cd9bc0105146df016c7a70b67469966419f5785c405f95aef56e36605e25ae93ae59f58b56333a73135244cd6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Victor
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
# GoodServices
|
2
|
+
|
3
|
+
This is a very simple gem I've created to keep the logic I've been frequently using in my apps to standardize my Service Objects. I intend to keep it as simple as possible so it can be versatile and easy to use, but ideas to improve it and pull-requests are welcome :)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'good_services'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install good_services
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
*The best way to understand this gem is to have a look at the [GoodServices::Base](/victoryam/good_services/blob/master/lib/good_services/base.rb) file. It is the only file that matters for this gem (really).*
|
26
|
+
|
27
|
+
To get started just create a new class inheriting from **GoodServices::Base**
|
28
|
+
|
29
|
+
#### Sample Service Object:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
module Pet
|
33
|
+
#
|
34
|
+
# Errors
|
35
|
+
#
|
36
|
+
InvalidPetError = Class.new(StandardError)
|
37
|
+
NoNewNameError = Class.new(StandardError)
|
38
|
+
|
39
|
+
#
|
40
|
+
# Service
|
41
|
+
#
|
42
|
+
class Renamer < GoodServices::Base
|
43
|
+
rescuable_from InvalidPetError, NoNewNameError
|
44
|
+
attr_reader :new_name
|
45
|
+
|
46
|
+
def initialize(pet: pet, new_name: new_name)
|
47
|
+
@record = pet
|
48
|
+
@new_name = new_name
|
49
|
+
end
|
50
|
+
|
51
|
+
def perform
|
52
|
+
@record.name = new_name
|
53
|
+
end
|
54
|
+
|
55
|
+
def validate
|
56
|
+
raise InvalidPetError unless record.respond_to? :name
|
57
|
+
raise NoNewNameError unless new_name
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
```
|
63
|
+
|
64
|
+
|
65
|
+
#### By default the following variables are available as attr_readers:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# Used when the service operates on only one object
|
69
|
+
@record
|
70
|
+
|
71
|
+
# Used when the service operates on a collection of objects
|
72
|
+
@collection
|
73
|
+
|
74
|
+
# If you run the service with the safe ".run" method, the @error
|
75
|
+
# variable will be filled with any rescuable error defined in the service.
|
76
|
+
@error
|
77
|
+
```
|
78
|
+
|
79
|
+
--
|
80
|
+
|
81
|
+
### Rescuable Exceptions
|
82
|
+
|
83
|
+
You can define a list of exceptions that are actually expected to happen eventually and the safe ".run" method will rescue you out of them returning **false** instaead.
|
84
|
+
|
85
|
+
The exception will be saved in the **@error** variable and accessible to the outside for custom error messages and etc. Here is how you use it:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class Renamer < GoodServices::Base
|
89
|
+
rescuable_from InvalidPetError, NoNewNameError
|
90
|
+
(...)
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
--
|
95
|
+
|
96
|
+
### Methods
|
97
|
+
|
98
|
+
#### Perform
|
99
|
+
The only required method for your service is the **perform** method.
|
100
|
+
It will have the logic called by your .run and .run! methods.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
def perform
|
104
|
+
@record.name = new_name
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Validate
|
109
|
+
This method is optional, but will be called before your perform method when you ".run" or ".run!" the service.
|
110
|
+
It should raise predictable errors based on your validations, for example:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
def validate
|
114
|
+
raise InvalidPetError unless record.respond_to? :name
|
115
|
+
raise NoNewNameError unless new_name
|
116
|
+
end
|
117
|
+
```
|
118
|
+
**This syntax is not my masterpiece, I appreciate any ideas of a better way to do this.*
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
## Development
|
123
|
+
|
124
|
+
**To-do**
|
125
|
+
|
126
|
+
- Improve the way validations are done (right now the "raise x if y" stuff is kinda ugly)
|
127
|
+
|
128
|
+
## Contributing
|
129
|
+
|
130
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/victoryam/good_services.
|
131
|
+
|
132
|
+
|
133
|
+
## License
|
134
|
+
|
135
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
136
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "good_services"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'good_services/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "good_services"
|
8
|
+
spec.version = GoodServices::VERSION
|
9
|
+
spec.date = '2016-07-12'
|
10
|
+
spec.authors = ["Victor A.M."]
|
11
|
+
spec.email = ["victor@victoryam.com.br"]
|
12
|
+
|
13
|
+
spec.summary = %q{A simple structure to help you build your service objects better and faster}
|
14
|
+
spec.description = %q{A simple structure to help you build your service objects better and faster}
|
15
|
+
spec.homepage = "https://github.com/victoryam/good_services"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module GoodServices
|
2
|
+
class Base
|
3
|
+
attr_reader :record, :collection, :error
|
4
|
+
|
5
|
+
#
|
6
|
+
# Inheritance wizardry
|
7
|
+
#
|
8
|
+
class << self
|
9
|
+
attr_accessor :rescuable_list, :default_rescues
|
10
|
+
|
11
|
+
DEFAULT_RESCUES = []
|
12
|
+
|
13
|
+
def inherit_variables(rescuable_list, default_rescues)
|
14
|
+
@rescuable_list = rescuable_list || []
|
15
|
+
@default_rescues = default_rescues || DEFAULT_RESCUES
|
16
|
+
end
|
17
|
+
|
18
|
+
def inherited(subclass)
|
19
|
+
subclass.inherit_variables(@rescuable_list, @default_rescues)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Helpers for instancing and running the Service
|
25
|
+
#
|
26
|
+
def self.run(*args)
|
27
|
+
self.new(*args).run
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.run!(*args)
|
31
|
+
self.new(*args).run!
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Runs the validations and perform methods
|
36
|
+
# Returns true or raise an exception
|
37
|
+
#
|
38
|
+
def run!
|
39
|
+
validate
|
40
|
+
perform
|
41
|
+
return true
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Runs the validations and perform methods rescuing from known exceptions
|
46
|
+
# Returns either true or false
|
47
|
+
#
|
48
|
+
def run
|
49
|
+
validate
|
50
|
+
perform
|
51
|
+
return true
|
52
|
+
rescue *self.class.acceptable_exceptions => @error
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
|
56
|
+
def validate
|
57
|
+
end
|
58
|
+
|
59
|
+
def perform
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# This configuration method adds custom exceptions to the rescuable list
|
64
|
+
#
|
65
|
+
def self.rescuable_from(*rescuable_exceptions)
|
66
|
+
@rescuable_list = rescuable_exceptions
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# This method returns either the set rescuable_from or the defaults
|
71
|
+
#
|
72
|
+
def self.acceptable_exceptions
|
73
|
+
(@rescuable_list + @default_rescues).uniq
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: good_services
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Victor A.M.
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: A simple structure to help you build your service objects better and
|
56
|
+
faster
|
57
|
+
email:
|
58
|
+
- victor@victoryam.com.br
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- good_services.gemspec
|
72
|
+
- lib/good_services.rb
|
73
|
+
- lib/good_services/base.rb
|
74
|
+
- lib/good_services/version.rb
|
75
|
+
homepage: https://github.com/victoryam/good_services
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: A simple structure to help you build your service objects better and faster
|
99
|
+
test_files: []
|