Class: ModuleComponentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/module_component/module_component_generator.rb

Constant Summary collapse

COMPONENT_TYPES =
%w[controller model serializer service].freeze

Instance Method Summary collapse

Instance Method Details

#comp_nameObject (private)



42
43
44
45
# File 'lib/generators/module_component/module_component_generator.rb', line 42

def comp_name
  methods_hash = methods.to_h
  @comp_name = methods_hash['component_name'] || file_name
end

#create_componentObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/module_component/module_component_generator.rb', line 17

def create_component
  # Take each passed in argument (e.g.) controller, serializer, etc
  # and create the corresponding files within the module for each arg
  path = "modules/#{file_name}/app"
  methods_hash = methods.to_h
  method = methods_hash['method']
  component_name = methods_hash['component_name'] || file_name

  if COMPONENT_TYPES.include? method
    template_name = method == 'model' ? "#{component_name}.rb" : "#{component_name}_#{method}.rb"
    template "app/#{method.pluralize}/#{method}.rb.erb",
             File.join(path, method.pluralize.to_s, file_name, 'v0', template_name.to_s), comp_name

    if method == 'service'
      template "app/#{method.pluralize}/configuration.rb.erb",
               File.join(path, method.pluralize.to_s, file_name, 'v0', 'configuration.rb'), comp_name
    end
  else
    $stdout.puts "\n#{method} is not a known generator command."\
                 "Commands allowed are controller, model, serializer and service\n"
  end
end

#prompt_userObject



11
12
13
14
15
# File 'lib/generators/module_component/module_component_generator.rb', line 11

def prompt_user
  if !Dir.exist?("modules/#{file_name}") && yes?("Module #{file_name} does not exist. Would you like to create it?")
    `rails g module #{file_name}`
  end
end