action_service 1.1.0 → 2.0.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
  SHA256:
3
- metadata.gz: 0e557d577a7c6be44d862d4929a2df8ee053d2834aa705374df3d740c5b64aa0
4
- data.tar.gz: 56acbc897cc072d3740bebe22b79ae5cd86b47f1ee4075a61d167cb70e966214
3
+ metadata.gz: b1380fb36f26ce37de08cac1a1234a2ba69526a93e8481975bc342f89ffef03d
4
+ data.tar.gz: 0b060a1922cf2994daa803e881180b2c7c6a55a03100c5c28599a98b16784e51
5
5
  SHA512:
6
- metadata.gz: cfe05a34ab7c13a4efa95d8f56135ef4bcd319d0bfb31580eb240e7f7bb3963b131da0eb8f13b639685f0f90f49e5919e5a42442ed1c48327d41f6d7f31db75f
7
- data.tar.gz: 956dddc06eee6edcaf6d4ea23be309b6110d894ee52958e70db6b1889ec5af0177f18f2454349e568e22863dbf6b26d78f8404d1f2367f7fb0a942ca7b088cd4
6
+ metadata.gz: 961f51fd421624c0d486c7da5f4a73e65c38fc6b99daaf9abf8f5dfb52f15e51ac15ec70e5e95e61f4ac2fcfabe19fa7c409fcae71eb9a5bfa2ad19b09eb3a8d
7
+ data.tar.gz: e1f3e3399f218967483bc612edbe059cbe87d22979cb290f3fa6304d9f9677fe4e4da584c2380725892c006d54d15c9fa39c8246e6d99420b5dd2fc4db5e3932
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # ActionService v1.0
1
+ # ActionService v2.x
2
2
  Welcome to Action Service gem, is a ruby gem to create and interact easily with services.
3
3
 
4
4
  ## Why services
5
- Is where you can add your code to perform simple functionality instead of making complex controllers or models.
5
+ This is where you can add your code to perform a simple function instead of making complex controllers or models.
6
6
 
7
7
  ## Installation
8
8
  Add this line to your application's Gemfile:
@@ -22,7 +22,7 @@ $ gem install action_service
22
22
  ```
23
23
 
24
24
  ## Setting up
25
- For start to use the gem execute:
25
+ To start to use the gem execute:
26
26
  ```bash
27
27
  $ rails g service:application_service
28
28
  ```
@@ -42,84 +42,79 @@ Running via Spring preloader in process 30051
42
42
  And when you open `authenticate_service.rb` will be like this
43
43
  ```ruby
44
44
  class Admin::AuthenticateService < ApplicationService
45
+ def initialize()
46
+ super()
47
+ end
45
48
 
46
- def initialize()
47
- super()
48
- end
49
-
50
- def call
51
- return self
52
- end
53
-
49
+ def call
50
+ self
51
+ end
54
52
  end
55
53
  ```
56
54
 
57
55
  ### ApplicationService class
58
- All generated services are inhariting from `ApplcaitonService`, `ApplicationService` is inherite from `ActoinService::Base, So all service will contain:
56
+ All generated services are inheriting from `ApplcaitonService`, `ApplicationService` is inherited from `ActoinService::Base, So all service will contain:
59
57
  #### Three instance variables
60
- 1. `@success` is a boolean with `true` as default value , and if you add any error this flag will be changed to `false`.
61
- 2. `@errors` is a array contain a list if errors.
58
+ 1. `@success` is a boolean with `true` as the default value, and if you add any error this flag will be changed to `false`.
59
+ 2. `@errors` is an array containing a list of errors.
62
60
  3. `@response` is a hash to add service response (values, objects, etc.).
63
61
  #### Six instance methods
64
- 1. `success?` to get boolean if service excuted successfully ot not (based on adding errors).
65
- 2. `errors` to get list of errors.
66
- 3. `response` to get response hash.
62
+ 1. `success?` to get boolean if service is executed successfully or not (based on adding errors).
63
+ 2. `errors` to get a list of errors.
64
+ 3. `response` to get the response hash.
67
65
  4. `add_error(error_message)` to add error (will change `@success` to `false`), Example `add_error("wrong admin id")`.
68
66
  5. `add_errors(*error_messages)` to add errors as parameters or array (will change `@success` to `false`), Example `add_errors("email is required","phone number is aready exist")` OR `add_errors(["email is required","phone number is aready exist"])`.
69
- 6. `add_errors_array(error_messages_array)` to add array of errors (will change `@success` to `false`), Example `add_errors_array(["email is required","phone number is aready exist"])`.
70
67
 
71
68
  Now, let's implement `Admin::AuthenticateService`
72
69
  ```ruby
73
70
  class Admin::AuthenticateService < ApplicationService
74
-
75
- def initialize(email, password)
76
- super()
77
- @admin = Admin.find_by(email: email)
78
- @password = password
79
- end
80
-
81
- def call
82
- add_error "wrong admin email" and return self if not @admin
83
- add_error "wrong password" and return self if not @admin.authenticate(@password)
84
- @response[:admin] = @admin
85
- return self
86
- end
71
+ def initialize(email, password)
72
+ super()
73
+ @admin = Admin.find_by(email: email)
74
+ @password = password
75
+ end
76
+
77
+ def call
78
+ add_error "wrong admin email" and return self if not @admin
79
+ add_error "wrong password" and return self if not @admin.authenticate(@password)
80
+ @response[:admin] = @admin
81
+ self
82
+ end
87
83
  end
88
84
  ```
89
85
 
90
- This service is now ready to be used, For example we will call the service inside a controller
86
+ This service is now ready to be used, For example, we will call the service inside a controller
91
87
  ```ruby
92
88
  result = Admin::AuthenticateService.new(params[:email], params[:password]).call
93
89
  if result.success?
94
- # {success: true, response: {admin object}, errors: []}
95
- render json: { success: result.success?, response: result.response, errors: result.errors }
96
- else:
97
- # {success: false, response: {}, errors: ["wrong email" OR "wrong password"]}
98
- render json: { success: result.success?, response: result.response, errors: result.errors }, status: :unauthorized
90
+ # {success: true, response: {admin object}, errors: []}
91
+ render json: { success: result.success?, response: result.response, errors: result.errors }
92
+ else
93
+ # {success: false, response: {}, errors: ["wrong email" OR "wrong password"]}
94
+ render json: { success: result.success?, response: result.response, errors: result.errors }, status: :unauthorized
99
95
  end
100
96
  ```
101
97
 
102
- #### Services layer will help you to write clean code, by small models, controller and DRY code (don't repeat yourself), you can use service inside another service and stop excuting the first service if the second one fails, Example:
98
+ #### Services layer will help you to write clean code, by small models, controller and DRY code (don't repeat yourself), you can use service inside another service and stop executing the first service if the second one fails, Example:
103
99
  ```ruby
104
100
  class Cache::List::AddHashService < ApplicationService
105
-
106
- def initialize(list_key, hash_key, hash_data, expiry_datetime=nil)
107
- super()
108
- @list_key = list_key
109
- @hash_key = hash_key
110
- @hash = hash_data
111
- @expiry_datetime = expiry_datetime
112
- end
113
-
114
- def call
115
- result = Cache::Hash::SetService.new(hash_key, hash_data, @expiry_datetime).call
116
- # if Cache::Hash::SetService fails will stop excuting and return the errors
117
- add_errors result.errors and return self if not result.success?
118
- result = Cache::List::AddService.new(list_key, hash_key, @expiry_datetime).call
119
- # if Cache::List::AddService fails will stop excuting and return the errors
120
- add_errors result.errors and return self if not result.success?
121
- self
122
- end
101
+ def initialize(list_key, hash_key, hash_data, expiry_datetime=nil)
102
+ super()
103
+ @list_key = list_key
104
+ @hash_key = hash_key
105
+ @hash = hash_data
106
+ @expiry_datetime = expiry_datetime
107
+ end
108
+
109
+ def call
110
+ result = Cache::Hash::SetService.new(hash_key, hash_data, @expiry_datetime).call
111
+ # if Cache::Hash::SetService fails will stop excuting and return the errors
112
+ add_errors result.errors and return self if not result.success?
113
+ result = Cache::List::AddService.new(list_key, hash_key, @expiry_datetime).call
114
+ # if Cache::List::AddService fails will stop excuting and return the errors
115
+ add_errors result.errors and return self if not result.success?
116
+ self
117
+ end
123
118
  end
124
119
  ```
125
120
 
@@ -127,4 +122,4 @@ end
127
122
  Bug reports and pull requests are welcome on GitHub at [https://github.com/abdofawzi5/action_service](https://github.com/abdofawzi5/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.
128
123
 
129
124
  ## License
130
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
125
+ The gem is available as open-source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -4,24 +4,16 @@ require 'generators/service/service_generator'
4
4
  require 'generators/application_service/application_service_generator'
5
5
 
6
6
  module ActionService
7
-
8
-
9
7
  # ActionService::Base is the parent class for all services that will be generated.
10
8
  class Base
11
- def initialize
9
+ attr_accessor :errors, :response
10
+
11
+ def initialize
12
12
  @errors = [] # contain errors
13
13
  @response = {} # contain the service response data
14
14
  @success = true # flag service execution without any error
15
15
  end
16
16
 
17
- def errors
18
- @errors
19
- end
20
-
21
- def response
22
- @response
23
- end
24
-
25
17
  def success?
26
18
  @success
27
19
  end
@@ -33,18 +25,8 @@ module ActionService
33
25
 
34
26
  def add_errors(*error_messages)
35
27
  @success = false
36
- if error_messages[0].kind_of?(Array)
37
- # called using array
38
- add_errors_array(error_messages[0])
39
- else
40
- @errors += error_messages
41
- end
42
- end
43
-
44
- def add_errors_array(error_messages_array)
45
- @success = false
46
- @errors += error_messages_array
28
+ error_messages = error_messages[0] if error_messages[0].is_a?
29
+ @errors += error_messages
47
30
  end
48
31
  end
49
-
50
32
  end
@@ -1,3 +1,3 @@
1
1
  module ActionService
2
- VERSION = '1.1.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -1,18 +1,15 @@
1
1
  module Service
2
2
  class ApplicationServiceGenerator < Rails::Generators::Base
3
-
4
- source_root File.expand_path("templates", __dir__)
3
+ source_root File.expand_path('templates', __dir__)
5
4
 
6
5
  def create_application_service
7
- template "application_service.rb", application_service_file_name
6
+ template 'application_service.rb', application_service_file_name
8
7
  end
9
8
 
10
9
  private
11
10
 
12
11
  def application_service_file_name
13
- @application_record_file_name ||= "app/services/application_service.rb"
12
+ @application_service_file_name ||= 'app/services/application_service.rb'
14
13
  end
15
-
16
-
17
14
  end
18
- end
15
+ end
@@ -1,6 +1,5 @@
1
1
  module Service
2
2
  class ServiceGenerator < Rails::Generators::NamedBase
3
-
4
3
  source_root File.expand_path('templates', __dir__)
5
4
 
6
5
  def create_service_file
@@ -10,12 +9,11 @@ module Service
10
9
  private
11
10
 
12
11
  def file_name
13
- @_file_name ||= remove_possible_suffix(super)
12
+ @file_name ||= remove_possible_suffix(super)
14
13
  end
15
14
 
16
15
  def remove_possible_suffix(name)
17
- name.sub(/_?service$/i, "")
16
+ name.sub(/_?service$/i, '')
18
17
  end
19
-
20
18
  end
21
19
  end
@@ -1,13 +1,11 @@
1
1
  <% module_namespacing do -%>
2
2
  class <%= class_name %>Service < ApplicationService
3
+ def initialize()
4
+ super()
5
+ end
3
6
 
4
- def initialize()
5
- super()
6
- end
7
-
8
- def call
9
- return self
10
- end
11
-
7
+ def call
8
+ self
9
+ end
12
10
  end
13
11
  <% end -%>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdo Fawzi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-21 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Is where you can add your code to do simple functionality instead of
14
14
  make complex controller or models.
@@ -34,7 +34,7 @@ homepage: https://github.com/abdofawzi5/action_service
34
34
  licenses:
35
35
  - MIT
36
36
  metadata: {}
37
- post_install_message:
37
+ post_install_message:
38
38
  rdoc_options: []
39
39
  require_paths:
40
40
  - lib
@@ -49,9 +49,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0'
51
51
  requirements: []
52
- rubyforge_project:
53
- rubygems_version: 2.7.6.2
54
- signing_key:
52
+ rubygems_version: 3.1.4
53
+ signing_key:
55
54
  specification_version: 4
56
55
  summary: Is a ruby gem to create and interact easily with services.
57
56
  test_files: []