plain_service 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +3 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +7 -0
- data/lib/plain_service/base.rb +35 -0
- data/lib/plain_service/version.rb +3 -0
- data/lib/plain_service.rb +5 -0
- data/plain_service.gemspec +24 -0
- data/spec/plain_service/base_spec.rb +55 -0
- data/spec/spec_helper.rb +2 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cc597e6835083bc0cd92c2d93857f4f30b4aa96f
|
4
|
+
data.tar.gz: 5a2636f49606ec957874ac45ea1ef09570ec47df
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 01589ce872ad275d1f48c0157ec035380d3adc3e1a3e9dc82668bfc9565d0144f57c2002f64955d7292c02cecb208c3812bd055df0f5ecdbec7b0d0e6bf437aa
|
7
|
+
data.tar.gz: 2b5692a39468b23c9246b630abff2aadbd8e2efb931ae9840e013266dc5191d1c2f11328e6a4d1a06dedb971ec645d89a7eddb7167493ec51a912a021767a28c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 yratanov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# PlainService
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'plain_service'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install plain_service
|
16
|
+
|
17
|
+
## Why?
|
18
|
+
|
19
|
+
To keep your controller and model thin.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Example (simple password change service):
|
24
|
+
|
25
|
+
1. Define your service:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# app/services/user_service/change_password
|
29
|
+
|
30
|
+
module UserService
|
31
|
+
class ChangePassword
|
32
|
+
attribute :user
|
33
|
+
attribute :password
|
34
|
+
attribute :password_confirmation
|
35
|
+
|
36
|
+
validates :password, presence: true, confirmation: true
|
37
|
+
|
38
|
+
def process
|
39
|
+
user.password = password
|
40
|
+
user.save
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
2. Use it in your controller:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
def change_password
|
49
|
+
change_password = UserService::ChangePassword.new(params)
|
50
|
+
change_password.user = current_user
|
51
|
+
|
52
|
+
# you can also call UserService::ChangePassword.perform(params) as shortcut
|
53
|
+
|
54
|
+
if change_password.perform
|
55
|
+
render #good response
|
56
|
+
else
|
57
|
+
render #bad response, see change_password.errors to get standard rails Errors object
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Advantages of such approach:
|
63
|
+
|
64
|
+
1. Simple and readable controller code. All action-related logic will be in service object.
|
65
|
+
2. Validations that are related with specific action also hidden in service to keep model clean and easy to use.
|
66
|
+
3. Easy to test. No callback hell, just plain ruby object.
|
67
|
+
|
68
|
+
# Passing errors from model to service
|
69
|
+
|
70
|
+
Sometimes you want to have some specific model errors in service object. For example you are saving model with uniqueness validation on some field. Then you can do the following:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
def process
|
74
|
+
if user.save
|
75
|
+
true
|
76
|
+
else
|
77
|
+
pass_errors_from user
|
78
|
+
false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# usage
|
83
|
+
service.perform
|
84
|
+
service.errors # => will include all errors from user object
|
85
|
+
```
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork it
|
90
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
91
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
92
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
93
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
module PlainService
|
5
|
+
class Base
|
6
|
+
include Virtus.model
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
def self.perform(params)
|
10
|
+
new(params).perform
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
if valid?
|
15
|
+
process
|
16
|
+
else
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Should return whether true or false value depending on success/fail status of service object actions
|
22
|
+
def process
|
23
|
+
raise NotImplementedError('You should implement #process method in your child class')
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# Use it if you want to delegate errors from underlying object to service
|
29
|
+
def pass_errors_from(object)
|
30
|
+
object.errors.each do |key, error|
|
31
|
+
errors.add key, error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'plain_service/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "plain_service"
|
8
|
+
spec.version = PlainService::VERSION
|
9
|
+
spec.authors = ["yratanov"]
|
10
|
+
spec.email = ["organium@gmail.com"]
|
11
|
+
spec.description = "Plain Service object implementation"
|
12
|
+
spec.summary = "Base class for your service objects"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PlainService::Base do
|
4
|
+
subject { described_class.new(params) }
|
5
|
+
let(:params) { {} }
|
6
|
+
|
7
|
+
context '#perform' do
|
8
|
+
context 'valid' do
|
9
|
+
before do
|
10
|
+
allow(subject).to receive(:valid?).and_return true
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'process succeeds' do
|
14
|
+
before do
|
15
|
+
allow(subject).to receive(:process).and_return true
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be truthy' do
|
19
|
+
expect(subject.perform).to be_truthy
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'process fails' do
|
24
|
+
before do
|
25
|
+
allow(subject).to receive(:process).and_return false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be falsey' do
|
29
|
+
expect(subject.perform).to be_falsey
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'invalid' do
|
35
|
+
before do
|
36
|
+
allow(subject).to receive(:valid?).and_return false
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should be falsey' do
|
40
|
+
expect(subject.perform).to be_falsey
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context '.perform' do
|
46
|
+
it 'should instantiate and perform' do
|
47
|
+
expect(PlainService::Base).
|
48
|
+
to receive(:new).with(params).and_call_original
|
49
|
+
expect_any_instance_of(PlainService::Base).
|
50
|
+
to receive(:perform).and_return true
|
51
|
+
|
52
|
+
expect(described_class.perform(params)).to be_truthy
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: plain_service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yratanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Plain Service object implementation
|
56
|
+
email:
|
57
|
+
- organium@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/plain_service.rb
|
69
|
+
- lib/plain_service/base.rb
|
70
|
+
- lib/plain_service/version.rb
|
71
|
+
- plain_service.gemspec
|
72
|
+
- spec/plain_service/base_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Base class for your service objects
|
98
|
+
test_files:
|
99
|
+
- spec/plain_service/base_spec.rb
|
100
|
+
- spec/spec_helper.rb
|