Class: Rsodx::Cli::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rsodx/cli/commands/generator.rb

Constant Summary collapse

ROOT_MAP =
{
  controller: "app/controllers",
  presenter:  "app/presenters",
  serializer: "app/serializers"
}
SUFFIX_MAP =
{
  controller: "Controller",
  presenter:  "Presenter",
  serializer: "Serializer"
}

Class Method Summary collapse

Class Method Details

.camelize(str) ⇒ Object



35
36
37
# File 'lib/rsodx/cli/commands/generator.rb', line 35

def self.camelize(str)
  str.split(/[_-]/).map { |part| part.capitalize }.join
end

.generate(type, args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rsodx/cli/commands/generator.rb', line 17

def self.generate(type, args)
  path = args[:path]
  type = type.to_sym
  raise "Unknown type: #{type}" unless ROOT_MAP.key?(type)

  segments     = path.split("/")
  file_name    = segments.pop
  file_path    = "#{ROOT_MAP[type]}/#{segments.join("/")}/#{file_name}_#{type}.rb"
  namespace    = segments.map { |seg| camelize(seg) }.join("::")
  class_name   = "#{camelize(file_name)}#{SUFFIX_MAP[type]}"
  full_class   = [namespace, class_name].reject(&:empty?).join("::")

  FileUtils.mkdir_p(File.dirname(file_path))
  File.write(file_path, template(full_class, type))

  puts "✅ Created #{file_path}"
end

.template(full_class, type) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rsodx/cli/commands/generator.rb', line 39

def self.template(full_class, type)
  base_class = {
    controller: "AppController",
    presenter:  "AppPresenter",
    serializer: "AppSerializer"
  }[type]

  namespace = full_class.split("::")[0..-2].join("::")
  class_name = full_class.split("::").last

  <<~RUBY
    module #{namespace}
      class #{class_name} < #{base_class}
        def call
          # TODO: implement
        end
      end
    end
  RUBY
end