Class: ModuleGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ModuleHelper
Defined in:
lib/generators/module/module_generator.rb

Instance Method Summary collapse

Methods included from ModuleHelper

#module_generator_file_insert

Instance Method Details

#create_additional_filesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/module/module_generator.rb', line 25

def create_additional_files
  path = "modules/#{file_name}"

  # create rakefile
  template 'Rakefile.erb', File.join(path, 'Rakefile')

  # create readme
  template 'README.rdoc.erb', File.join(path, 'README.rdoc')

  # create bin/rails
  template 'bin/rails.erb', File.join(path, 'bin', 'rails')
  chmod File.join(path, 'bin', 'rails'), 0o755

  # /spec/spec_helper
  template 'spec/spec_helper.rb.erb', File.join(path, 'spec', 'spec_helper.rb')

  # create the routes file
  template 'config/routes.rb.erb', File.join(path, 'config', 'routes.rb')

  # create gemspec
  template 'gemspec.erb', File.join(path, "#{file_name}.gemspec")

  # create gemfile
  template 'Gemfile.erb', File.join(path, 'Gemfile')
end

#create_directory_structureObject



10
11
12
13
14
15
# File 'lib/generators/module/module_generator.rb', line 10

def create_directory_structure
  # create the dir structure here
  %w[controllers models serializers services].each do |dir|
    FileUtils.mkdir_p "modules/#{file_name}/app/#{dir}" unless Dir.exist?("modules/#{file_name}/app/#{dir}")
  end
end

#create_engineObject



17
18
19
20
21
22
23
# File 'lib/generators/module/module_generator.rb', line 17

def create_engine
  # create engine file
  path = "modules/#{file_name}/lib"
  template 'lib/namespace/engine.rb.erb', File.join(path, file_name, 'engine.rb')
  template 'lib/namespace/version.rb.erb', File.join(path, file_name, 'version.rb')
  template 'lib/namespace.rb.erb', File.join(path, "#{file_name}.rb")
end

#update_and_installObject

:nocov:



92
93
94
95
# File 'lib/generators/module/module_generator.rb', line 92

def update_and_install
  run 'bundle install'
  puts "\n\u{1F64C} new module generated at ./modules/#{file_name}\n\n\n"
end

#update_gemfileObject



74
75
76
77
78
79
80
# File 'lib/generators/module/module_generator.rb', line 74

def update_gemfile
  options_hash = {}
  options_hash[:insert_matcher] = "gem '#{file_name}'"
  options_hash[:new_entry] = "  #{options_hash[:insert_matcher]}\n"

  module_generator_file_insert('Gemfile', options_hash)
end

#update_routes_fileObject



82
83
84
85
86
87
88
89
# File 'lib/generators/module/module_generator.rb', line 82

def update_routes_file
  options_hash = {}
  options_hash[:regex] = /# Modules(.*)# End Modules/m
  options_hash[:insert_matcher] = "mount #{file_name.camelize}::Engine, at: '/#{file_name}'"
  options_hash[:new_entry] = "  mount #{file_name.camelize}::Engine, at: '/#{file_name}'\n"

  module_generator_file_insert('config/routes.rb', options_hash)
end

#update_simplecov_helperObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/module/module_generator.rb', line 62

def update_simplecov_helper
  # spec and simplecov helper add group

  options_hash = {}
  options_hash[:regex] = /# Modules(.*)end/m
  options_hash[:insert_matcher] = "add_group '#{file_name.camelize}', 'modules/#{file_name}/'"
  options_hash[:new_entry] = "    add_group '#{file_name.camelize}', " \
                             "'modules/#{file_name}/'\n"

  module_generator_file_insert('spec/simplecov_helper.rb', options_hash)
end

#update_spec_helperObject

rubocop:disable Rails/Output



52
53
54
55
56
57
58
59
60
# File 'lib/generators/module/module_generator.rb', line 52

def update_spec_helper
  options_hash = {}
  options_hash[:regex] = /# Modules(.*)# End Modules/m
  options_hash[:insert_matcher] = "add_group '#{file_name.camelize}', 'modules/#{file_name}/'"
  options_hash[:new_entry] = "    add_group '#{file_name.camelize}', " \
                             "'modules/#{file_name}/'\n"

  module_generator_file_insert('spec/spec_helper.rb', options_hash)
end