Class: ApplicationServiceGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/application_service_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_application_service_fileObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/generators/application_service_generator.rb', line 6

def create_application_service_file
  service_path = 'app/services/application_service.rb'

  if File.exist?(service_path)
    # File exists, check if it needs to be updated
    content = File.read(service_path)

    unless content.include?('class ApplicationService < ServiceBase::Service')
      # Update the class definition
      new_content = content.sub(/class ApplicationService.*\n/, "class ApplicationService < ServiceBase::Service\n")
      File.write(service_path, new_content)
    end
  else
    # Create new file with template
    create_file service_path, <<~RUBY
      class ApplicationService < ServiceBase::Service
      end
    RUBY
  end
end