command_service_object 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/CONTRIBUTING.md +92 -0
- data/Gemfile.lock +3 -1
- data/README.md +69 -39
- data/command_service_object.gemspec +12 -3
- data/lib/command_service_object/configuration.rb +14 -0
- data/lib/command_service_object/failure.rb +10 -0
- data/lib/command_service_object/helpers/controller_helper.rb +30 -0
- data/lib/command_service_object/helpers/failure_helper.rb +7 -0
- data/lib/command_service_object/helpers/model_helper.rb +49 -0
- data/lib/command_service_object/hooks.rb +87 -0
- data/lib/command_service_object/railtie.rb +13 -0
- data/lib/command_service_object/version.rb +1 -1
- data/lib/command_service_object.rb +25 -2
- data/lib/generators/service/command/USAGE +8 -0
- data/lib/generators/service/command/command_generator.rb +29 -0
- data/lib/generators/service/{templates → command/templates}/command.rb.erb +8 -1
- data/lib/generators/service/getter/getter_generator.rb +32 -0
- data/lib/generators/service/getter/templates/getter.rb.erb +8 -0
- data/lib/generators/service/install/install_generator.rb +23 -0
- data/lib/generators/service/install/templates/initializer.rb +19 -0
- data/lib/generators/service/install/templates/services/application_service.rb +35 -0
- data/lib/generators/service/install/templates/services/case_base.rb +13 -0
- data/lib/generators/service/{templates/base → install/templates/services}/service_result.rb +0 -0
- data/lib/generators/service/service_generator.rb +32 -96
- data/lib/generators/service/setter/setter_generator.rb +32 -0
- data/lib/generators/service/setter/templates/setter.rb.erb +12 -0
- data/lib/generators/service/setup/setup_generator.rb +21 -0
- data/lib/generators/service/test/USAGE +8 -0
- data/lib/generators/service/{templates → test/templates}/minitest.rb.erb +0 -0
- data/lib/generators/service/{templates → test/templates}/rspec.rb.erb +0 -0
- data/lib/generators/service/test/test_generator.rb +56 -0
- data/lib/generators/service/usecase/USAGE +8 -0
- data/lib/generators/service/{templates → usecase/templates}/usecase.rb.erb +7 -6
- data/lib/generators/service/usecase/usecase_generator.rb +29 -0
- metadata +53 -13
- data/lib/generators/service/templates/base/application_service.rb +0 -9
- data/lib/generators/service/templates/base/service_base.rb +0 -37
- data/lib/generators/service/templates/base/service_controller_helper.rb +0 -25
- data/lib/generators/service/templates/error.rb.erb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7d55b12da58695d99604fb284d14f73a1373a6b637c1de67ec8917b3573b54f
|
4
|
+
data.tar.gz: '09b44ad7cf634686a9e7d6acd73f831428352c0f68fbef97b904eac46ad6a603'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ede9b963f6a0bcfe6339e7b4b55b07bab3425e1751c7d6a838a5211e408f8f6c901dea81c11c01deedf799ddce6ce9bc4fe3437be3d320ecf4945f83d8c57edb
|
7
|
+
data.tar.gz: a540e5fc63419308287bc5280d2648bdbcaa3b83ed30c30af9d944e5830c96875c043bc196315bf1da01b20fe2df01574b2d57580baccdab4b4182a42eb08e4c
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [v0.4.0](https://github.com/adham90/command_service_object/tree/v0.4.0) (2019-04-12)
|
4
|
+
**Closed issues:**
|
5
|
+
|
6
|
+
- Generating Specs [\#1](https://github.com/adham90/command_service_object/issues/1)
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
When contributing to this repository, please first discuss the change you wish to make via issue,
|
4
|
+
email, or any other method with the owners of this repository before making a change.
|
5
|
+
|
6
|
+
Please note we have a code of conduct, please follow it in all your interactions with the project.
|
7
|
+
|
8
|
+
## Pull Request Process
|
9
|
+
|
10
|
+
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
|
11
|
+
build.
|
12
|
+
2. Update the README.md with details of changes to the interface, this includes new environment
|
13
|
+
variables, exposed ports, useful file locations and container parameters.
|
14
|
+
3. Increase the version numbers in any examples files and the README.md to the new version that this
|
15
|
+
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
|
16
|
+
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
|
17
|
+
do not have permission to do that, you may request the second reviewer to merge it for you.
|
18
|
+
|
19
|
+
## Code of Conduct
|
20
|
+
|
21
|
+
### Our Pledge
|
22
|
+
|
23
|
+
In the interest of fostering an open and welcoming environment, we as
|
24
|
+
contributors and maintainers pledge to making participation in our project and
|
25
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
26
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
27
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
28
|
+
orientation.
|
29
|
+
|
30
|
+
### Our Standards
|
31
|
+
|
32
|
+
Examples of behavior that contributes to creating a positive environment
|
33
|
+
include:
|
34
|
+
|
35
|
+
* Using welcoming and inclusive language
|
36
|
+
* Being respectful of differing viewpoints and experiences
|
37
|
+
* Gracefully accepting constructive criticism
|
38
|
+
* Focusing on what is best for the community
|
39
|
+
* Showing empathy towards other community members
|
40
|
+
|
41
|
+
Examples of unacceptable behavior by participants include:
|
42
|
+
|
43
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
44
|
+
advances
|
45
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
46
|
+
* Public or private harassment
|
47
|
+
* Publishing others' private information, such as a physical or electronic
|
48
|
+
address, without explicit permission
|
49
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
50
|
+
professional setting
|
51
|
+
|
52
|
+
### Our Responsibilities
|
53
|
+
|
54
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
55
|
+
behavior and are expected to take appropriate and fair corrective action in
|
56
|
+
response to any instances of unacceptable behavior.
|
57
|
+
|
58
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
59
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
60
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
61
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
62
|
+
threatening, offensive, or harmful.
|
63
|
+
|
64
|
+
### Scope
|
65
|
+
|
66
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
67
|
+
when an individual is representing the project or its community. Examples of
|
68
|
+
representing a project or community include using an official project e-mail
|
69
|
+
address, posting via an official social media account, or acting as an appointed
|
70
|
+
representative at an online or offline event. Representation of a project may be
|
71
|
+
further defined and clarified by project maintainers.
|
72
|
+
|
73
|
+
### Enforcement
|
74
|
+
|
75
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
76
|
+
reported by contacting the project team at [INSERT EMAIL ADDRESS]. All
|
77
|
+
complaints will be reviewed and investigated and will result in a response that
|
78
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
79
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
80
|
+
Further details of specific enforcement policies may be posted separately.
|
81
|
+
|
82
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
83
|
+
faith may face temporary or permanent repercussions as determined by other
|
84
|
+
members of the project's leadership.
|
85
|
+
|
86
|
+
### Attribution
|
87
|
+
|
88
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
89
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
90
|
+
|
91
|
+
[homepage]: http://contributor-covenant.org
|
92
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
command_service_object (0.
|
4
|
+
command_service_object (0.5.0)
|
5
5
|
virtus (~> 1.0.5)
|
6
6
|
|
7
7
|
GEM
|
@@ -54,6 +54,7 @@ GEM
|
|
54
54
|
ice_nine (~> 0.11.0)
|
55
55
|
thread_safe (~> 0.3, >= 0.3.1)
|
56
56
|
builder (3.2.3)
|
57
|
+
byebug (9.0.6)
|
57
58
|
coercible (1.0.0)
|
58
59
|
descendants_tracker (~> 0.0.1)
|
59
60
|
concurrent-ruby (1.1.5)
|
@@ -135,6 +136,7 @@ PLATFORMS
|
|
135
136
|
|
136
137
|
DEPENDENCIES
|
137
138
|
bundler (~> 2.0)
|
139
|
+
byebug (~> 9.0.6)
|
138
140
|
command_service_object!
|
139
141
|
minitest (~> 5.11, >= 5.11.3)
|
140
142
|
rails (~> 5.0)
|
data/README.md
CHANGED
@@ -25,7 +25,6 @@ Add this line to your application's Gemfile:
|
|
25
25
|
```ruby
|
26
26
|
gem 'command_service_object'
|
27
27
|
```
|
28
|
-
|
29
28
|
And then execute:
|
30
29
|
|
31
30
|
$ bundle
|
@@ -34,73 +33,104 @@ Or install it yourself as:
|
|
34
33
|
|
35
34
|
$ gem install command_service_object
|
36
35
|
|
36
|
+
Next, you need to run the generator:
|
37
|
+
|
38
|
+
```bash
|
39
|
+
$ rails generate service:install
|
40
|
+
```
|
41
|
+
|
37
42
|
## Usage
|
38
43
|
|
39
44
|
$ rails g service [service_name] [usecases usecases]
|
40
|
-
###
|
45
|
+
### Generate Service ex:
|
41
46
|
|
42
|
-
$ rails g service
|
47
|
+
$ rails g service auth login
|
43
48
|
output
|
44
49
|
|
45
50
|
```bash
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
app/services/
|
52
|
+
├── application_service.rb
|
53
|
+
├── auth_service
|
54
|
+
│ ├── commands
|
55
|
+
│ │ └── login.rb
|
56
|
+
│ └── usecases
|
57
|
+
│ ├── login.rb
|
58
|
+
│ ├── login.rb
|
59
|
+
│ ├── getters
|
60
|
+
│ └── setters
|
61
|
+
│ └── user_profile_image.rb
|
62
|
+
├── case_base.rb
|
63
|
+
└── service_result.rb
|
56
64
|
```
|
65
|
+
|
66
|
+
### Generate setters and getters ex:
|
67
|
+
|
68
|
+
$ rails g service:setter auth user_profile_image
|
69
|
+
$ rails g service:getter auth user_balance
|
70
|
+
|
57
71
|
then you can edit command params
|
58
72
|
> you can read [Virtus gem docs](https://github.com/solnic/virtus) for more info.
|
59
73
|
```ruby
|
60
|
-
# app/services/
|
61
|
-
|
62
|
-
|
74
|
+
# app/services/auth_service/commands/login.rb
|
75
|
+
# frozen_string_literal: true
|
76
|
+
|
77
|
+
module AuthService::Commands
|
78
|
+
class Login
|
79
|
+
# You can read Virtus gem doc for more info.
|
80
|
+
# https://github.com/solnic/virtus
|
63
81
|
include Virtus.model
|
82
|
+
include ActiveModel::Validations
|
64
83
|
|
65
|
-
|
66
|
-
attribute :
|
67
|
-
|
84
|
+
# Attributes
|
85
|
+
# attribute :REPLACE_ME, String
|
86
|
+
|
87
|
+
# Validations
|
88
|
+
# validates :REPLACE_ME, presence: true
|
68
89
|
end
|
69
90
|
end
|
70
91
|
```
|
71
92
|
and then add your business logic
|
72
93
|
```ruby
|
73
|
-
# app/services/
|
74
|
-
|
75
|
-
|
94
|
+
# app/services/auth_service/usecases/login.rb
|
95
|
+
# frozen_string_literal: true
|
96
|
+
|
97
|
+
module AuthService::Usecases
|
98
|
+
class Login < CaseBase
|
99
|
+
include CommandServiceObject::Hooks
|
100
|
+
setters :user_profile_image
|
101
|
+
getters :user_balance
|
102
|
+
#
|
103
|
+
# Your business logic goes here, keep [call] method clean by using private
|
104
|
+
# methods for Business logic.
|
105
|
+
#
|
76
106
|
def call
|
77
|
-
|
78
|
-
|
79
|
-
do_something
|
80
|
-
do_another_something
|
107
|
+
result = user_profile_image(image_url) # set user profile image ex.
|
108
|
+
balance = user_balance # get user balance ex.
|
81
109
|
end
|
82
110
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
# Business logic
|
87
|
-
# Don't catch errors ApplicationService will do that for you
|
88
|
-
raise Errors::CustomeError if ERROR
|
111
|
+
# This method will run if call method raise error
|
112
|
+
def rollback
|
113
|
+
# rollback logic
|
89
114
|
end
|
90
115
|
|
91
|
-
|
92
|
-
|
93
|
-
|
116
|
+
private
|
117
|
+
|
118
|
+
def replace_me
|
119
|
+
# [business logic]
|
120
|
+
end
|
94
121
|
end
|
95
122
|
end
|
123
|
+
|
96
124
|
```
|
97
125
|
|
98
126
|
usage from controller
|
99
127
|
```ruby
|
100
|
-
class
|
101
|
-
|
102
|
-
|
103
|
-
|
128
|
+
class AuthenticationController < ApplicationController
|
129
|
+
default_service :auth_service
|
130
|
+
|
131
|
+
def Login
|
132
|
+
cmd = command.new(params) # AuthService::Commands::Login.new
|
133
|
+
result = execute(cmd)
|
104
134
|
|
105
135
|
if result.ok?
|
106
136
|
render json: result.value!.as_json, status: 201
|
@@ -5,8 +5,8 @@ require 'command_service_object/version'
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'command_service_object'
|
7
7
|
spec.version = CommandServiceObject::VERSION
|
8
|
-
spec.authors = ['Adham EL-Deeb']
|
9
|
-
spec.email = ['adham.eldeeb90@gmail.com']
|
8
|
+
spec.authors = ['Adham EL-Deeb', 'Mohamed Diaa']
|
9
|
+
spec.email = ['adham.eldeeb90@gmail.com', 'mohamed.diaa27@gmail.com']
|
10
10
|
|
11
11
|
spec.summary = 'Rails Generator for command service object.'
|
12
12
|
spec.description = 'command_service_object gem helps you to generate'\
|
@@ -20,15 +20,24 @@ Gem::Specification.new do |spec|
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
spec.metadata = {
|
24
|
+
'homepage_uri' => 'https://github.com/adham90/command_service_object',
|
25
|
+
'changelog_uri' => 'https://github.com/adham90/command_service_object/blob/master/CHANGELOG.md',
|
26
|
+
'source_code_uri' => 'https://github.com/adham90/command_service_object',
|
27
|
+
'bug_tracker_uri' => 'https://github.com/adham90/command_service_object/issues'
|
28
|
+
}
|
29
|
+
|
23
30
|
spec.bindir = 'exe'
|
24
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
32
|
spec.require_paths = ['lib']
|
33
|
+
spec.post_install_message = 'Yaaay!! CommandServiceObject is ready to rock your app!!'
|
26
34
|
|
27
35
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
36
|
+
spec.add_development_dependency 'byebug', '~> 9.0.6'
|
28
37
|
spec.add_development_dependency 'minitest', '~> 5.11', '>= 5.11.3'
|
29
38
|
spec.add_development_dependency 'rails', '~> 5.0'
|
30
39
|
spec.add_development_dependency 'rake', '~> 10.0'
|
31
40
|
spec.add_development_dependency 'thor', '~> 0.20.3'
|
32
41
|
|
33
|
-
spec.
|
42
|
+
spec.add_dependency 'virtus', '~> 1.0.5'
|
34
43
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module CommandServiceObject
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :append_controller_helper, :command_method_name
|
4
|
+
attr_accessor :skip_command, :skip_test, :skip_error
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@append_controller_helper = true
|
8
|
+
@command_method_name = :command
|
9
|
+
@skip_command = false
|
10
|
+
@skip_test = false
|
11
|
+
@skip_error = true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CommandServiceObject
|
4
|
+
module ControllerHelper
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveSupport.on_load :action_controller do
|
10
|
+
define_method(CommandServiceObject.configuration.command_method_name) \
|
11
|
+
do |service: nil, usecase: action_name|
|
12
|
+
service_name = service || self.class.default_service || controller_name
|
13
|
+
|
14
|
+
"#{service_name}/commands/#{usecase}".camelize.constantize
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute(cmd)
|
19
|
+
ApplicationService.call(cmd)
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
@default_service = nil
|
24
|
+
|
25
|
+
def default_service(service_name = nil)
|
26
|
+
@default_service ||= service_name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CommandServiceObject
|
4
|
+
module ModelHelper
|
5
|
+
def model_name
|
6
|
+
name.camelize
|
7
|
+
end
|
8
|
+
|
9
|
+
def model_class
|
10
|
+
Object.const_get(model_name)
|
11
|
+
rescue StandardError
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def model_attributes
|
16
|
+
return { REPLACE_ME: String } if model_class.nil?
|
17
|
+
|
18
|
+
attrs = {}
|
19
|
+
model_class.columns_hash.each do |k, v|
|
20
|
+
next if ignored_column_names.include?(k)
|
21
|
+
|
22
|
+
type = allowed_column_types[v.type]
|
23
|
+
next if type.nil?
|
24
|
+
|
25
|
+
attrs[k] = type
|
26
|
+
end
|
27
|
+
attrs
|
28
|
+
end
|
29
|
+
|
30
|
+
def ignored_column_names
|
31
|
+
%w[
|
32
|
+
created_at
|
33
|
+
updated_at
|
34
|
+
encrypted_password
|
35
|
+
]
|
36
|
+
end
|
37
|
+
|
38
|
+
def allowed_column_types
|
39
|
+
{
|
40
|
+
string: 'String',
|
41
|
+
bigint: 'Integer',
|
42
|
+
integer: 'Integer',
|
43
|
+
decimal: 'Float',
|
44
|
+
boolean: 'Boolean',
|
45
|
+
datetime: 'DateTime'
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module CommandServiceObject
|
2
|
+
module Hooks
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ClassMethods
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
|
7
|
+
class << base
|
8
|
+
alias_method :_new, :new
|
9
|
+
|
10
|
+
define_method :new do |command|
|
11
|
+
_new(command).tap do |instance|
|
12
|
+
instance.send(:setup_setters, _setters)
|
13
|
+
instance.send(:setup_getters, _getters)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
attr_accessor :_called_setters
|
21
|
+
|
22
|
+
def _called_setters
|
23
|
+
@_called_setters ||= []
|
24
|
+
end
|
25
|
+
|
26
|
+
def rollback_setters
|
27
|
+
_called_setters.reverse_each(&:rollback)
|
28
|
+
end
|
29
|
+
|
30
|
+
def setup_getters(getters)
|
31
|
+
getters.each do |getter|
|
32
|
+
method_name = getter.name.split('::').last.underscore
|
33
|
+
|
34
|
+
define_singleton_method(method_name) do |payload|
|
35
|
+
getter.new.call(args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def setup_setters(setters)
|
41
|
+
setters.each do |setter|
|
42
|
+
method_name = setter.name.split('::').last.underscore
|
43
|
+
|
44
|
+
# unrollable setters
|
45
|
+
define_singleton_method("#{method_name}!") do |payload|
|
46
|
+
setter.new(payload).call
|
47
|
+
end
|
48
|
+
|
49
|
+
# rollable setters
|
50
|
+
define_singleton_method(method_name) do |payload|
|
51
|
+
obj = setter.new(payload)
|
52
|
+
obj.call
|
53
|
+
|
54
|
+
_called_setters << obj
|
55
|
+
obj
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module ClassMethods
|
62
|
+
cattr_accessor :_getters, :_setters
|
63
|
+
|
64
|
+
def _setters
|
65
|
+
@_setters ||= Set.new([])
|
66
|
+
end
|
67
|
+
|
68
|
+
def _getters
|
69
|
+
@_getters ||= Set.new([])
|
70
|
+
end
|
71
|
+
|
72
|
+
def setters(name)
|
73
|
+
service = to_s.split('::').first
|
74
|
+
obj = "#{service}/Usecases/Setters/#{name}".camelize.constantize
|
75
|
+
|
76
|
+
_setters.add(obj)
|
77
|
+
end
|
78
|
+
|
79
|
+
def getters(name)
|
80
|
+
service = to_s.split('::').first
|
81
|
+
obj = "#{service}/Usecases/Getters/#{name}".camelize.constantize
|
82
|
+
|
83
|
+
_getters.add(obj)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module CommandServiceObject
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer 'configure_rails_initialization' do |_app|
|
4
|
+
ActiveSupport.on_load :action_controller do
|
5
|
+
CommandServiceObject::Railtie.setup_action_controller
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.setup_action_controller
|
10
|
+
ActionController::Base.send :include, ControllerHelper
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -1,6 +1,29 @@
|
|
1
1
|
require 'command_service_object/version'
|
2
|
+
require 'command_service_object/configuration'
|
3
|
+
require 'command_service_object/failure'
|
4
|
+
require 'command_service_object/helpers/model_helper'
|
5
|
+
require 'command_service_object/helpers/controller_helper'
|
6
|
+
require 'command_service_object/helpers/failure_helper'
|
7
|
+
require 'command_service_object/hooks'
|
8
|
+
|
9
|
+
if defined?(Rails) && Rails::VERSION::STRING >= '3.0'
|
10
|
+
require 'command_service_object/railtie'
|
11
|
+
end
|
2
12
|
|
3
13
|
module CommandServiceObject
|
4
|
-
class
|
5
|
-
|
14
|
+
class << self
|
15
|
+
attr_accessor :configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.reset
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure
|
27
|
+
yield(configuration)
|
28
|
+
end
|
6
29
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../setup/setup_generator.rb'
|
2
|
+
|
3
|
+
module Service
|
4
|
+
module Generators
|
5
|
+
class CommandGenerator < Rails::Generators::NamedBase
|
6
|
+
include CommandServiceObject::ModelHelper
|
7
|
+
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
argument :commands, type: :array, default: [], banner: 'command command'
|
11
|
+
|
12
|
+
def call
|
13
|
+
invoke Service::Generators::SetupGenerator, [name]
|
14
|
+
@model_attributes = model_attributes
|
15
|
+
commands.each do |c|
|
16
|
+
@command = c.classify
|
17
|
+
path = "app/services/#{service_name}/commands/#{c.underscore}.rb"
|
18
|
+
template 'command.rb.erb', path unless options.skip_command?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def service_name
|
25
|
+
"#{name.underscore}_service"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -5,7 +5,14 @@ module <%= service_name.classify %>::Commands
|
|
5
5
|
# You can read Virtus gem doc for more info.
|
6
6
|
# https://github.com/solnic/virtus
|
7
7
|
include Virtus.model
|
8
|
+
include ActiveModel::Validations
|
8
9
|
|
9
|
-
|
10
|
+
# Attributes
|
11
|
+
<%- @model_attributes.each do |k,v| -%>
|
12
|
+
# attribute :<%=k%>, <%=v%>
|
13
|
+
<%- end -%>
|
14
|
+
|
15
|
+
# Validations
|
16
|
+
# validates :REPLACE_ME, presence: true
|
10
17
|
end
|
11
18
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../setup/setup_generator.rb'
|
2
|
+
|
3
|
+
module Service
|
4
|
+
module Generators
|
5
|
+
class GetterGenerator < Rails::Generators::NamedBase
|
6
|
+
check_class_collision
|
7
|
+
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
argument :getters, type: :array, default: [], banner: 'getter getter'
|
11
|
+
|
12
|
+
def setup
|
13
|
+
invoke Service::Generators::SetupGenerator, [name]
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_getters
|
17
|
+
getters.each do |g|
|
18
|
+
@getter = g.classify
|
19
|
+
|
20
|
+
path = "app/services/#{service_name}/usecases/getters/#{g.underscore}.rb"
|
21
|
+
template 'getter.rb.erb', path
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def service_name
|
28
|
+
"#{name.underscore}_service"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|