command_service_object 0.3.2 → 0.4.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26e3e53d35598d601bf546fc5046c751a0572dd6c936b8e2ded9acfa131699b9
|
4
|
+
data.tar.gz: 93bedb8abb7dd172b52190a51a0978ff963ff812afe5da51f8bb3422d79b8b8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5f85e59bd82b841981354719f59fb7e6062887e3dffe42946b18461cfaa5df8e8fcc4d7a223d0436f5a1164be8510dc065aac1c6785b434b4dc3884300a64c5
|
7
|
+
data.tar.gz: 2e368552a3f24ad456a5cbcf3f255a7b7f335765a5ca6c57f162188300cd1fada63aba45396ef648b958ce90eaf898f67ce95f97721ae8a27f41dfd31c6a92e3
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,22 @@
|
|
2
2
|
|
3
3
|
Rails Generator for command service object.
|
4
4
|
|
5
|
+
### Theory:
|
6
|
+
[Command Design Pattern](https://en.wikipedia.org/wiki/Command_pattern) consists of `Command Object` and `Service Object` (Executor), Command object is responsible for containing `Client` requests and run input validations on it to ensure that the request is valid and set default values, then `Service Object` applies the business logic on that command.
|
7
|
+
|
8
|
+
### Implementation:
|
9
|
+
Service consists of several objects { `Command Object` ` Usecase Object` And `Error Object` (business logic error) }.
|
10
|
+
|
11
|
+
- **Command Object:** the object that responsible for containing `Client` requests and run input validations it's implemented using [Virtus](https://github.com/solnic/virtus) gem and can use `activerecord` for validations and it's existed under `commands` dir.
|
12
|
+
- **Usecase Object:** this object responsible for executing the business logic, Every `usecase` should execute one command type only so that command name should be the same as usecase object name, usecase object existed under 'usecases` dir.
|
13
|
+
- **Error Object:** business logic errors existed user `errors` dir inside the service dir.
|
14
|
+
|
15
|
+
#### Result Object:
|
16
|
+
In case of successful or failure `ApplicationService` the responsible object for all services will return `service_result` object this object contain `value!` method containing successful call result, and `errors` method containing failure `errors` objects.
|
17
|
+
|
18
|
+
> You can check if the result successful or not by using `ok?` method.
|
19
|
+
|
20
|
+
|
5
21
|
## Installation
|
6
22
|
|
7
23
|
Add this line to your application's Gemfile:
|
@@ -22,6 +22,22 @@ class ServiceGenerator < Rails::Generators::NamedBase
|
|
22
22
|
directory 'base', 'app/services'
|
23
23
|
end
|
24
24
|
|
25
|
+
def add_controller_helper_to_application_controller
|
26
|
+
application_controller_path = 'app/controllers/application_controller.rb'
|
27
|
+
|
28
|
+
line = File.readlines(application_controller_path).select do |li|
|
29
|
+
li =~ /class ApplicationController </
|
30
|
+
end.first
|
31
|
+
|
32
|
+
inject_into_file application_controller_path, after: line do
|
33
|
+
" include ServiceControllerHelper\n"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def add_virtus_gem_to_gemfile
|
38
|
+
gem 'virtus'
|
39
|
+
end
|
40
|
+
|
25
41
|
def create_service_dir
|
26
42
|
return if File.exist?("app/services/#{service_name}")
|
27
43
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ServiceControllerHelper
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
def command(service: nil, usecase: action_name)
|
9
|
+
service_name = service || self.class.default_service || controller_name
|
10
|
+
|
11
|
+
"#{service_name}/commands/#{usecase}".camelize.constantize
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute(cmd)
|
15
|
+
ApplicationService.call(cmd)
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
@default_service = nil
|
20
|
+
|
21
|
+
def default_service(service_name = nil)
|
22
|
+
@default_service ||= service_name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_service_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adham EL-Deeb
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/generators/service/service_generator.rb
|
127
127
|
- lib/generators/service/templates/base/application_service.rb
|
128
128
|
- lib/generators/service/templates/base/service_base.rb
|
129
|
+
- lib/generators/service/templates/base/service_controller_helper.rb
|
129
130
|
- lib/generators/service/templates/base/service_result.rb
|
130
131
|
- lib/generators/service/templates/command.rb.erb
|
131
132
|
- lib/generators/service/templates/error.rb.erb
|