Class: TypesGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_types_fileObject



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

def create_types_file
  types_path = 'app/models/type.rb'

  if File.exist?(types_path)
    # File exists, check if it needs the include statement
    content = File.read(types_path)

    unless content.include?('include ServiceBase::Types')
      # Add include statement at the top of the module
      new_content = content.sub(/module Type\s*\n/, "module Type\n  include ServiceBase::Types\n")
      File.write(types_path, new_content)
    end
  else
    # Create new file with template
    create_file types_path, <<~RUBY
      module Type
        include ServiceBase::Types
      end
    RUBY
  end
end