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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aee49210439832ac6fdee2deec56da0f008a97421c7d47e91b2b41564e78df21
4
- data.tar.gz: f1885b95dd77067e94d81f2e5b36c921fbbfb0c02b6ee5f95838f4c48c5f48ca
3
+ metadata.gz: e2217a3e17e64e6a5f301c65fa470347e767a4eefeb3d293558f46ef4c3979a2
4
+ data.tar.gz: 7d23e110c1d640ac4352e0c3b5a1d69ef97c8dbaf6b7ae46be3739ae33ce109e
5
5
  SHA512:
6
- metadata.gz: 63cae3bcf9626c733aa494e3154c1b2e4800ff43fa26fa0f2b7d8a957e47d7cfe90161724010ccfcc735f6dc28975e49cde84d233d78b3886ba57e20f4db9a2b
7
- data.tar.gz: 9c581f2cd67ae3128b9178e793164be2904fcc9cab624a8267738caebaa4b5e228565c0d817e562541be2f588ee618366de8968c9b0ee640d0f535dd9758c0f5
6
+ metadata.gz: 23d95efafcc1c6a36dc84b31d2593f81b1d10d1146a37b0f7d8c983b6a6a4f692491fbf1b395ba9aa8029964b7fd07dc44fe66e6f226dcbc1414a9531faa2761
7
+ data.tar.gz: 95e06753cce2f0360522bc2f3cb7cbba3eb21df3c83f1c1c279a5b2902fc5ea140ae66c935cf994da0ec854d61a2ff05629fed8c8e1b09c70a8a818de7c9b17e
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # CommandServiceObject
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/command_service_object`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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/[USERNAME]/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.
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/[USERNAME]/command_service_object/blob/master/CODE_OF_CONDUCT.md).
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).
@@ -1,3 +1,3 @@
1
1
  module CommandServiceObject
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -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,6 +2,8 @@
2
2
 
3
3
  module <%= service_name.classify %>::Commands
4
4
  class <%= @usecase.classify %>
5
+ # You can read Virtus gem doc for more info.
6
+ # https://github.com/solnic/virtus
5
7
  include Virtus.model
6
8
 
7
9
  attribute :REPLACE_ME, String
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= service_name.classify %>::Errors
4
+ class <%= @usecase.classify %> < StandardError
5
+ def initialize(msg = "custom message")
6
+ super(msg)
7
+ end
8
+ end
9
+ end
@@ -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.1.1
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: