command_service_object 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +77 -6
- data/lib/command_service_object/version.rb +1 -1
- data/lib/generators/service/service_generator.rb +2 -0
- data/lib/generators/service/templates/command.rb.erb +2 -0
- data/lib/generators/service/templates/error.rb.erb +9 -0
- data/lib/generators/service/templates/usecase.rb.erb +11 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2217a3e17e64e6a5f301c65fa470347e767a4eefeb3d293558f46ef4c3979a2
|
4
|
+
data.tar.gz: 7d23e110c1d640ac4352e0c3b5a1d69ef97c8dbaf6b7ae46be3739ae33ce109e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23d95efafcc1c6a36dc84b31d2593f81b1d10d1146a37b0f7d8c983b6a6a4f692491fbf1b395ba9aa8029964b7fd07dc44fe66e6f226dcbc1414a9531faa2761
|
7
|
+
data.tar.gz: 95e06753cce2f0360522bc2f3cb7cbba3eb21df3c83f1c1c279a5b2902fc5ea140ae66c935cf994da0ec854d61a2ff05629fed8c8e1b09c70a8a818de7c9b17e
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# CommandServiceObject
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Rails Generator for command service object.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,80 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
$ rails g service [service_name] [usecases usecases]
|
24
|
+
### Example
|
25
|
+
|
26
|
+
$ rails g service user create update delete
|
27
|
+
output
|
28
|
+
|
29
|
+
```bash
|
30
|
+
create app/services/user_service
|
31
|
+
create app/services/user_service/usecases
|
32
|
+
create app/services/user_service/commands
|
33
|
+
create app/services/user_service/errors
|
34
|
+
create app/services/user_service/usecases/create.rb
|
35
|
+
create app/services/user_service/commands/create.rb
|
36
|
+
create app/services/user_service/usecases/update.rb
|
37
|
+
create app/services/user_service/commands/update.rb
|
38
|
+
create app/services/user_service/usecases/delete.rb
|
39
|
+
create app/services/user_service/commands/delete.rb
|
40
|
+
```
|
41
|
+
then you can edit command params
|
42
|
+
> you can read [Virtus gem docs](https://github.com/solnic/virtus) for more info.
|
43
|
+
```ruby
|
44
|
+
# app/services/user_service/commands/create.rb
|
45
|
+
module UserService::Commands
|
46
|
+
class Create
|
47
|
+
include Virtus.model
|
48
|
+
|
49
|
+
attribute :name, String
|
50
|
+
attribute :phone, String
|
51
|
+
attribute :age, Integer
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
and then add your business logic
|
56
|
+
```ruby
|
57
|
+
# app/services/user_service/usecases/create.rb
|
58
|
+
module UserService::Usecases
|
59
|
+
class Create < ServiceBase
|
60
|
+
def call
|
61
|
+
# your business logic goes here
|
62
|
+
# keep call method clean by using private methods for Business logic
|
63
|
+
do_something
|
64
|
+
do_another_something
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def do_something
|
70
|
+
# Business logic
|
71
|
+
# Don't catch errors ApplicationService will do that for you
|
72
|
+
raise Errors::CustomeError if ERROR
|
73
|
+
end
|
74
|
+
|
75
|
+
def do_another_something
|
76
|
+
# another business logic
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
usage from controller
|
83
|
+
```ruby
|
84
|
+
class UsersController < ApplicationController
|
85
|
+
def create
|
86
|
+
cmd = UserService::Commands::Create.new(user_params)
|
87
|
+
result = ApplicationService.call(cmd)
|
88
|
+
|
89
|
+
if result.ok?
|
90
|
+
render json: result.value!.as_json, status: 201
|
91
|
+
else
|
92
|
+
render json: { message: result.error }, status: 422
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
26
97
|
|
27
98
|
## Development
|
28
99
|
|
@@ -32,7 +103,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
103
|
|
33
104
|
## Contributing
|
34
105
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/adham90/command_service_object. 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.
|
36
107
|
|
37
108
|
## License
|
38
109
|
|
@@ -40,4 +111,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
40
111
|
|
41
112
|
## Code of Conduct
|
42
113
|
|
43
|
-
Everyone interacting in the CommandServiceObject project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
114
|
+
Everyone interacting in the CommandServiceObject project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/adham90/command_service_object/blob/master/CODE_OF_CONDUCT.md).
|
@@ -25,9 +25,11 @@ class ServiceGenerator < Rails::Generators::NamedBase
|
|
25
25
|
@usecase = usecase
|
26
26
|
u_path = "app/services/#{service_name}/usecases/#{usecase.underscore}.rb"
|
27
27
|
c_path = "app/services/#{service_name}/commands/#{usecase.underscore}.rb"
|
28
|
+
e_path = "app/services/#{service_name}/errors/#{usecase.underscore}.rb"
|
28
29
|
|
29
30
|
template 'usecase.rb.erb', u_path
|
30
31
|
template 'command.rb.erb', c_path
|
32
|
+
template 'error.rb.erb', e_path
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
@@ -2,7 +2,18 @@
|
|
2
2
|
|
3
3
|
module <%= service_name.classify %>::Usecases
|
4
4
|
class <%= @usecase.classify %> < ServiceBase
|
5
|
+
#
|
6
|
+
# Your business logic goes here, keep [call] method clean by using private
|
7
|
+
# methods for Business logic.
|
8
|
+
#
|
5
9
|
def call
|
10
|
+
replace_me
|
6
11
|
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def replace_me
|
16
|
+
# [business logic]
|
17
|
+
end
|
7
18
|
end
|
8
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adham EL-Deeb
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/generators/service/USAGE
|
106
106
|
- lib/generators/service/service_generator.rb
|
107
107
|
- lib/generators/service/templates/command.rb.erb
|
108
|
+
- lib/generators/service/templates/error.rb.erb
|
108
109
|
- lib/generators/service/templates/usecase.rb.erb
|
109
110
|
homepage: https://github.com/adham90/command_service_object
|
110
111
|
licenses:
|