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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -0
  3. data/CONTRIBUTING.md +92 -0
  4. data/Gemfile.lock +3 -1
  5. data/README.md +69 -39
  6. data/command_service_object.gemspec +12 -3
  7. data/lib/command_service_object/configuration.rb +14 -0
  8. data/lib/command_service_object/failure.rb +10 -0
  9. data/lib/command_service_object/helpers/controller_helper.rb +30 -0
  10. data/lib/command_service_object/helpers/failure_helper.rb +7 -0
  11. data/lib/command_service_object/helpers/model_helper.rb +49 -0
  12. data/lib/command_service_object/hooks.rb +87 -0
  13. data/lib/command_service_object/railtie.rb +13 -0
  14. data/lib/command_service_object/version.rb +1 -1
  15. data/lib/command_service_object.rb +25 -2
  16. data/lib/generators/service/command/USAGE +8 -0
  17. data/lib/generators/service/command/command_generator.rb +29 -0
  18. data/lib/generators/service/{templates → command/templates}/command.rb.erb +8 -1
  19. data/lib/generators/service/getter/getter_generator.rb +32 -0
  20. data/lib/generators/service/getter/templates/getter.rb.erb +8 -0
  21. data/lib/generators/service/install/install_generator.rb +23 -0
  22. data/lib/generators/service/install/templates/initializer.rb +19 -0
  23. data/lib/generators/service/install/templates/services/application_service.rb +35 -0
  24. data/lib/generators/service/install/templates/services/case_base.rb +13 -0
  25. data/lib/generators/service/{templates/base → install/templates/services}/service_result.rb +0 -0
  26. data/lib/generators/service/service_generator.rb +32 -96
  27. data/lib/generators/service/setter/setter_generator.rb +32 -0
  28. data/lib/generators/service/setter/templates/setter.rb.erb +12 -0
  29. data/lib/generators/service/setup/setup_generator.rb +21 -0
  30. data/lib/generators/service/test/USAGE +8 -0
  31. data/lib/generators/service/{templates → test/templates}/minitest.rb.erb +0 -0
  32. data/lib/generators/service/{templates → test/templates}/rspec.rb.erb +0 -0
  33. data/lib/generators/service/test/test_generator.rb +56 -0
  34. data/lib/generators/service/usecase/USAGE +8 -0
  35. data/lib/generators/service/{templates → usecase/templates}/usecase.rb.erb +7 -6
  36. data/lib/generators/service/usecase/usecase_generator.rb +29 -0
  37. metadata +53 -13
  38. data/lib/generators/service/templates/base/application_service.rb +0 -9
  39. data/lib/generators/service/templates/base/service_base.rb +0 -37
  40. data/lib/generators/service/templates/base/service_controller_helper.rb +0 -25
  41. 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: 26e3e53d35598d601bf546fc5046c751a0572dd6c936b8e2ded9acfa131699b9
4
- data.tar.gz: 93bedb8abb7dd172b52190a51a0978ff963ff812afe5da51f8bb3422d79b8b8c
3
+ metadata.gz: e7d55b12da58695d99604fb284d14f73a1373a6b637c1de67ec8917b3573b54f
4
+ data.tar.gz: '09b44ad7cf634686a9e7d6acd73f831428352c0f68fbef97b904eac46ad6a603'
5
5
  SHA512:
6
- metadata.gz: b5f85e59bd82b841981354719f59fb7e6062887e3dffe42946b18461cfaa5df8e8fcc4d7a223d0436f5a1164be8510dc065aac1c6785b434b4dc3884300a64c5
7
- data.tar.gz: 2e368552a3f24ad456a5cbcf3f255a7b7f335765a5ca6c57f162188300cd1fada63aba45396ef648b958ce90eaf898f67ce95f97721ae8a27f41dfd31c6a92e3
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.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
- ### Example
45
+ ### Generate Service ex:
41
46
 
42
- $ rails g service user create update delete
47
+ $ rails g service auth login
43
48
  output
44
49
 
45
50
  ```bash
46
- create app/services/user_service
47
- create app/services/user_service/usecases
48
- create app/services/user_service/commands
49
- create app/services/user_service/errors
50
- create app/services/user_service/usecases/create.rb
51
- create app/services/user_service/commands/create.rb
52
- create app/services/user_service/usecases/update.rb
53
- create app/services/user_service/commands/update.rb
54
- create app/services/user_service/usecases/delete.rb
55
- create app/services/user_service/commands/delete.rb
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/user_service/commands/create.rb
61
- module UserService::Commands
62
- class Create
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
- attribute :name, String
66
- attribute :phone, String
67
- attribute :age, Integer
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/user_service/usecases/create.rb
74
- module UserService::Usecases
75
- class Create < ServiceBase
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
- # your business logic goes here
78
- # keep call method clean by using private methods for Business logic
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
- private
84
-
85
- def do_something
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
- def do_another_something
92
- # another business logic
93
- end
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 UsersController < ApplicationController
101
- def create
102
- cmd = UserService::Commands::Create.new(user_params)
103
- result = ApplicationService.call(cmd)
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.add_runtime_dependency 'virtus', '~> 1.0.5'
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,10 @@
1
+ module CommandServiceObject
2
+ class Failure < StandardError
3
+ attr_reader :extra_data
4
+
5
+ def initialize(message: nil, extra_data: {})
6
+ @extra_data = extra_data
7
+ super(message)
8
+ end
9
+ end
10
+ 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,7 @@
1
+ module CommandServiceObject
2
+ module FailureHelper
3
+ def fail!(message: '', extra_data: {})
4
+ raise Failure, message: message, extra_data: extra_data
5
+ end
6
+ end
7
+ 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,3 +1,3 @@
1
1
  module CommandServiceObject
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  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 Error < StandardError; end
5
- # Your code goes here...
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,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate service Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -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
- attribute :REPLACE_ME, String
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