Class: RailsForge::Generators::ServiceGenerator
- Inherits:
-
BaseGenerator
- Object
- BaseGenerator
- RailsForge::Generators::ServiceGenerator
- Defined in:
- lib/railsforge/generators/service_generator.rb
Overview
ServiceGenerator creates service objects
Defined Under Namespace
Classes: InvalidServiceNameError
Constant Summary
Constants inherited from BaseGenerator
BaseGenerator::TEMPLATE_VERSION
Class Method Summary collapse
-
.generate(service_name, with_spec: true, template_version: "v1") ⇒ Object
Class method for CLI.
Instance Method Summary collapse
-
#generate ⇒ String
Generate service file and optionally spec.
-
#generate_service_file ⇒ String
Generate the service file.
-
#generate_spec_file ⇒ String
Generate the spec file.
-
#service_template ⇒ String
Service template.
-
#spec_template ⇒ String
Spec template.
-
#validate_name!(name) ⇒ Object
Validates the service name.
Methods inherited from BaseGenerator
#camelize, #find_rails_app_path, #initialize, #underscore
Constructor Details
This class inherits a constructor from RailsForge::Generators::BaseGenerator
Class Method Details
.generate(service_name, with_spec: true, template_version: "v1") ⇒ Object
Class method for CLI
117 118 119 |
# File 'lib/railsforge/generators/service_generator.rb', line 117 def self.generate(service_name, with_spec: true, template_version: "v1") new(service_name, with_spec: with_spec, template_version: template_version).generate end |
Instance Method Details
#generate ⇒ String
Generate service file and optionally spec
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/railsforge/generators/service_generator.rb', line 21 def generate validate_name!(@name) unless @base_path raise "Not in a Rails application directory" end service_file_path = generate_service_file if [:with_spec] != false generate_spec_file end "Service '#{@name}' generated successfully!" end |
#generate_service_file ⇒ String
Generate the service file
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/railsforge/generators/service_generator.rb', line 39 def generate_service_file service_dir = File.join(@base_path, "app", "services") FileUtils.mkdir_p(service_dir) file_name = "#{underscore}.rb" file_path = File.join(service_dir, file_name) if File.exist?(file_path) puts " Skipping service (already exists)" return file_path end File.write(file_path, service_template) puts " Created app/services/#{file_name}" file_path end |
#generate_spec_file ⇒ String
Generate the spec file
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/railsforge/generators/service_generator.rb', line 58 def generate_spec_file spec_dir = File.join(@base_path, "spec", "services") FileUtils.mkdir_p(spec_dir) file_name = "#{underscore}_spec.rb" file_path = File.join(spec_dir, file_name) if File.exist?(file_path) puts " Skipping spec (already exists)" return file_path end File.write(file_path, spec_template) puts " Created spec/services/#{file_name}" file_path end |
#service_template ⇒ String
Service template
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/railsforge/generators/service_generator.rb', line 77 def service_template " # Service class for \#{underscore}\n # Encapsulates business logic related to \#{underscore}\n class \#{@name}\n def initialize(**args)\n @args = args\n end\n\n def call\n # TODO: Implement service logic here\n end\n\n def self.call(**args)\n new(**args).call\n end\n\n private\n end\n RUBY\nend\n" |
#spec_template ⇒ String
Spec template
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/railsforge/generators/service_generator.rb', line 101 def spec_template " # frozen_string_literal: true\n\n RSpec.describe \#{@name} do\n describe '#call' do\n it 'returns expected result' do\n result = described_class.call\n expect(result).to be_nil\n end\n end\n end\n RUBY\nend\n" |
#validate_name!(name) ⇒ Object
Validates the service name
15 16 17 |
# File 'lib/railsforge/generators/service_generator.rb', line 15 def validate_name!(name) super(name, /\A[A-Z][a-zA-Z0-9]*\z/, InvalidServiceNameError) end |