service_it 0.1.6 → 2.0.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: 614561e699e7f8c0ae4f2fba193f64318d5c68f8658fce2c5319cf92bc2194e5
4
- data.tar.gz: 3069a058004f09a212c2f759ceb13918757621aa9d621a1f1798391c37bea935
3
+ metadata.gz: 242be73eac2e2beb31caa752a64e14b3df89a8d5d1e86171ed0ebd17108363eb
4
+ data.tar.gz: f9bf6a0a347ea667a1b1d057edc866db2a7c720699bd2227ff251f71a1e212c7
5
5
  SHA512:
6
- metadata.gz: 49a2c89469386d1ad13489e0637dbf82f739e5dfe6c02eb166441d6b4e04c28c3d89e47588f8b0c71dc26bf7ae9c7f934ece6308e298189ec29264de3b347a13
7
- data.tar.gz: 61546e88499409d96b13bfb88405065e8a1b75c2e6b0f58343955d428e63daeb13a643472ce7777d0a24ee866f60a5cd85d7c0326dc89a3f5561307cac2f4275
6
+ metadata.gz: 63383bbf3934b6a25a3ed4bef403c0e21ad2c35abb5ba81914cd6fa010fa602d948e9868927a981ed0e0870df0767c5bf304bf8297f419d7c8bfb496950445e4
7
+ data.tar.gz: d44619f576bfb329151a241f40f589ff0c54248faf0bc9dce9b229bf0095a79cd6cc432feaf6ac95f3d11f9ce40483b49b84b982e3f4b57714493a8ccfe7a7ca
data/README.md CHANGED
@@ -1,67 +1,74 @@
1
- # ServiceIt
1
+ # ServiceIt
2
+ [![Gem Version](https://badge.fury.io/rb/service_it.svg)](https://badge.fury.io/rb/service_it) [![Build Status](https://travis-ci.org/iago-silva/service_it.svg?branch=master)](https://travis-ci.org/iago-silva/service_it) [![Code Climate](https://codeclimate.com/github/iago-silva/service_it.png)](https://codeclimate.com/github/iago-silva/service_it)
2
3
 
3
- [![Gem Version](https://badge.fury.io/rb/service_it.svg)](https://badge.fury.io/rb/service_it)
4
+ Its benefit is to facilitate the creation of Service Objects, providing you the basic and enough to have a complete one and letting you free to use on your own way.
4
5
 
5
- <div align="center">
6
- <img src="https://pbs.twimg.com/profile_images/1123677732/logo_avatar_large.png" height="250" width="250">
7
- <p>Its main objective is to provide you a way to delegate responsibilities in your project</p>
8
- </div>
9
-
10
- <br/>
6
+ - [ServiceIt](#serviceit)
7
+ - [Installation](#installation)
8
+ - [With Bundler](#with-bundler)
9
+ - [Rails Generator](#rails-generator)
10
+ - [Usage](#usage)
11
+ - [Example](#example)
11
12
 
12
13
  ## Installation
13
14
 
14
- Add this line to your application's Gemfile:
15
+ $ gem install service_it
16
+
17
+ ## With Bundler
15
18
 
16
- gem 'service_it', '0.1.6'
19
+ Add this line to your `Gemfile`:
20
+
21
+ gem 'service_it', '~> 1.2.0'
17
22
 
18
23
  And then execute:
19
24
 
20
25
  $ bundle
21
26
 
22
- Or install it yourself as:
27
+ ## Rails Generator
23
28
 
24
- $ gem install service_it
29
+ You can use Rails generator to create a `Service`
30
+
31
+ $ rails g service NAME
32
+
33
+ This will create:
34
+
35
+ ```
36
+ ├── app
37
+ ├── services
38
+    └── name.rb
39
+ ```
25
40
 
26
41
  ## Usage
27
42
 
28
- Inherit your class of `Service` and implement `perform` method
29
43
  ```ruby
30
- class Foo < Service
31
- def perform
32
- # here you can use params that became instance variables
33
- end
44
+ class Foo < ServiceIt::Base
45
+ def perform
46
+ # put your logic here
47
+ # you can use params that became variables
34
48
  end
49
+ end
35
50
  ```
36
51
 
37
- Call your service from anywhere
52
+ Call your service from anywhere (controllers, models, migrations, ...)
53
+
38
54
  ```ruby
39
- Foo.call(params)
40
- ```
55
+ Foo.call(foo: foo, bar: bar)
56
+ ```
41
57
 
42
- ## Example
58
+ ## Example
43
59
 
44
- Simple example to change status of a "transaction" to complete
60
+ Simple example to release a _POST_
45
61
 
46
62
  ```ruby
47
- # app/services/complete_transation_service.rb
48
- class CompleteTransation < Service
49
- def perform
50
- @transation.update(:status, :complete)
51
- end
52
- end
53
- ```
63
+ ReleasePost.call(post: @post)
64
+ ```
54
65
 
55
66
  ```ruby
56
- if CompleteTransation.call(transation: @transation)
57
- puts 'Transation complete!'
67
+ # app/services/release_post.rb
68
+ class ReleasePost < ServiceIt::Base
69
+ def perform
70
+ post.prepare_to_release
71
+ post.update(released_at: Date.current)
58
72
  end
59
- ```
60
-
61
- ## Contributing
62
-
63
- Bug reports and pull requests are welcome on GitHub at https://github.com/iago-silva/service_it.
64
-
65
- ## License
66
-
67
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
73
+ end
74
+ ```
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates a service
3
+
4
+ Example:
5
+ rails g service Foo
6
+
7
+ This will create:
8
+ app/services/foo.rb
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Generators
5
+ # Generates a service by rails generator
6
+ class ServiceGenerator < NamedBase
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def create_service_file
10
+ template 'service.rb',
11
+ File.join('app/services', class_path, "#{file_name}.rb")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ class <%= class_name %> < ServiceIt::Base
2
+ def perform
3
+ end
4
+ end
@@ -1 +1,3 @@
1
- require "service"
1
+ # frozen_string_literal: true
2
+
3
+ require 'service_it/base'
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ServiceIt
4
+ # Inherit this class and implement perform method
5
+ #
6
+ # Documentation:
7
+ # https://github.com/iago-silva/service_it
8
+ class Base
9
+ private_class_method :new
10
+
11
+ # Call your service
12
+ #
13
+ # Example:
14
+ # Foo.call(arg1: 1, arg2: 2)
15
+ #
16
+ # Arguments:
17
+ # args: (Hash)
18
+ #
19
+ # Return:
20
+ # perform's return
21
+ def self.call(**args)
22
+ new(args).perform
23
+ end
24
+
25
+ def initialize(args)
26
+ args.each do |key, value|
27
+ define_ivar(key, value)
28
+ define_private_reader(key)
29
+ end
30
+ end
31
+
32
+ # Implement this method to run your service
33
+ def perform
34
+ raise NotImplementedError,
35
+ "Please implement 'perform' method in your #{self.class.name}"
36
+ end
37
+
38
+ private
39
+
40
+ def define_ivar(key, value)
41
+ instance_variable_set("@#{key}", value)
42
+ end
43
+
44
+ def define_private_reader(key)
45
+ self.class.class_eval do
46
+ private
47
+
48
+ attr_reader key.to_sym
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ServiceIt
4
+ VERSION = '2.0.0'
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe ServiceIt::Base do
4
+ context 'when calls new method' do
5
+ subject { described_class.new }
6
+
7
+ it 'raises NoMethodError exception' do
8
+ expect { subject }.to raise_error(NoMethodError)
9
+ end
10
+ end
11
+
12
+ describe SayMyName do
13
+ context 'when I ask what is my name' do
14
+ subject { described_class.call(name: name) }
15
+
16
+ let(:name) { 'Heisenberg' }
17
+
18
+ it 'says Heisenberg' do
19
+ expect(subject).to eq(name)
20
+ end
21
+ end
22
+ end
23
+
24
+ describe MissedPerformService do
25
+ context 'when perform method is not defined' do
26
+ subject { described_class.call }
27
+
28
+ it 'raises NotImplementedError exception' do
29
+ expect { subject }.to raise_error(NotImplementedError)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe ServiceIt do
4
+ it 'has a version number' do
5
+ expect(ServiceIt::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
+
5
+ require 'service_it'
6
+
7
+ Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SayMyName < ServiceIt::Base
4
+ def perform
5
+ name
6
+ end
7
+ end
8
+
9
+ class MissedPerformService < ServiceIt::Base; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iago Silva
@@ -11,87 +11,87 @@ cert_chain: []
11
11
  date: 2018-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: pry
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.13.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.13.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 3.10.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 3.10.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: rubocop
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 1.5.1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 1.5.1
55
55
  - !ruby/object:Gem::Dependency
56
- name: pry
56
+ name: rubocop-rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 2.0.1
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 2.0.1
69
69
  description: |-
70
- Its main objective is to provide you a way to delegate
71
- responsibilities in your project
72
- email: iago.silva@interage.in
73
- executables:
74
- - console
75
- - setup
70
+ Service objects are a holy grail to keep your controllers and
71
+ models slim and readable
72
+ email:
73
+ - iago.cith@hotmail.com
74
+ executables: []
76
75
  extensions: []
77
76
  extra_rdoc_files: []
78
77
  files:
79
- - ".gitignore"
80
- - Gemfile
81
- - Gemfile.lock
82
- - LICENSE.txt
83
78
  - README.md
84
- - Rakefile
85
- - bin/console
86
- - bin/setup
87
- - lib/service.rb
79
+ - lib/generators/rails/USAGE
80
+ - lib/generators/rails/service_generator.rb
81
+ - lib/generators/rails/templates/service.rb
88
82
  - lib/service_it.rb
89
- - service_it.gemspec
90
- - spec/service_spec.rb
83
+ - lib/service_it/base.rb
84
+ - lib/service_it/version.rb
85
+ - spec/service_it/base_spec.rb
86
+ - spec/service_it_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/support/test_classes.rb
91
89
  homepage: https://github.com/iago-silva/service_it
92
90
  licenses:
93
91
  - MIT
94
- metadata: {}
92
+ metadata:
93
+ homepage_uri: https://github.com/iago-silva/service_it
94
+ source_code_uri: https://github.com/iago-silva/service_it
95
95
  post_install_message:
96
96
  rdoc_options: []
97
97
  require_paths:
@@ -100,18 +100,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: 2.4.0
104
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - ">="
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.7.3
110
+ rubygems_version: 3.1.2
112
111
  signing_key:
113
112
  specification_version: 4
114
- summary: A simple gem for Rails that allows you to isolate a snippet of code and call
115
- it from anywhere
113
+ summary: Simple gem to keep your controllers and models slim and readable
116
114
  test_files:
117
- - spec/service_spec.rb
115
+ - spec/service_it_spec.rb
116
+ - spec/support/test_classes.rb
117
+ - spec/service_it/base_spec.rb
118
+ - spec/spec_helper.rb
data/.gitignore DELETED
@@ -1,12 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- .idea
11
-
12
- *.gem
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
@@ -1,41 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- service_it (0.1.6)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- coderay (1.1.2)
10
- diff-lcs (1.3)
11
- method_source (0.9.2)
12
- pry (0.12.2)
13
- coderay (~> 1.1.0)
14
- method_source (~> 0.9.0)
15
- rake (12.3.2)
16
- rspec (3.7.0)
17
- rspec-core (~> 3.7.0)
18
- rspec-expectations (~> 3.7.0)
19
- rspec-mocks (~> 3.7.0)
20
- rspec-core (3.7.1)
21
- rspec-support (~> 3.7.0)
22
- rspec-expectations (3.7.0)
23
- diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.7.0)
25
- rspec-mocks (3.7.0)
26
- diff-lcs (>= 1.2.0, < 2.0)
27
- rspec-support (~> 3.7.0)
28
- rspec-support (3.7.1)
29
-
30
- PLATFORMS
31
- ruby
32
-
33
- DEPENDENCIES
34
- bundler
35
- pry
36
- rake
37
- rspec
38
- service_it!
39
-
40
- BUNDLED WITH
41
- 1.16.1
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2018 Iago Silva
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/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require 'rspec/core/rake_task'
2
- require 'bundler/gem_tasks'
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task default: :spec
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'service_it'
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
- require "pry"
10
- Pry.start
11
-
12
- require 'irb'
13
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,39 +0,0 @@
1
- # Inherit this class and implement perform method
2
- #
3
- # Documentation:
4
- # https://github.com/iago-silva/service_it#usage
5
- class Service
6
- class << self
7
- # Call your service
8
- #
9
- # Example:
10
- # ServiceIt.call(arg1: 1, arg2: 2)
11
- #
12
- # Arguments:
13
- # args: (Hash)
14
- #
15
- # Return:
16
- # perform's return
17
- def call(**args)
18
- new_instance(args).perform
19
- end
20
-
21
- private
22
-
23
- def new_instance(args)
24
- instance = new
25
-
26
- args.each do |key, value|
27
- instance.instance_variable_set("@#{key}", value)
28
- end
29
-
30
- instance
31
- end
32
- end
33
-
34
- # Implement this method to run your service
35
- def perform
36
- raise NotImplementedError,
37
- "Please implement 'perform' method in your #{self.class.name}"
38
- end
39
- end
@@ -1,26 +0,0 @@
1
- lib = File.expand_path('lib', __dir__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
-
4
- Gem::Specification.new do |s|
5
- s.name = 'service_it'
6
- s.version = '0.1.6'
7
- s.date = '2018-07-27'
8
- s.authors = ['Iago Silva']
9
- s.email = 'iago.silva@interage.in'
10
- s.homepage = 'https://github.com/iago-silva/service_it'
11
- s.license = 'MIT'
12
- s.summary = 'A simple gem for Rails that allows you to isolate a snippet
13
- of code and call it from anywhere'
14
- s.description = 'Its main objective is to provide you a way to delegate
15
- responsibilities in your project'
16
-
17
- s.files = `git ls-files -z`.split("\x0")
18
- s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
- s.require_paths = ['lib']
21
-
22
- s.add_development_dependency 'bundler'
23
- s.add_development_dependency 'rake'
24
- s.add_development_dependency 'rspec'
25
- s.add_development_dependency 'pry'
26
- end
@@ -1,17 +0,0 @@
1
- require 'service'
2
-
3
- describe Service do
4
- let(:result) { SayMyName.call(name: 'Heisenberg') }
5
-
6
- before do
7
- class SayMyName < Service
8
- def perform
9
- @name
10
- end
11
- end
12
- end
13
-
14
- it 'say Heisenberg' do
15
- expect(result).to eq('Heisenberg')
16
- end
17
- end