Class: Domino::ScaffoldRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/domino/scaffolder.rb

Overview

ScaffoldRunner: handles the actual file generation

Instance Method Summary collapse

Constructor Details

#initialize(model_name, columns, namespace, generate_model) ⇒ ScaffoldRunner

Returns a new instance of ScaffoldRunner.



25
26
27
28
29
30
# File 'lib/domino/scaffolder.rb', line 25

def initialize(model_name, columns, namespace, generate_model)
  @model_name = model_name
  @columns = columns
  @namespace = namespace
  @generate_model = generate_model
end

Instance Method Details

#column_argsObject



44
45
46
# File 'lib/domino/scaffolder.rb', line 44

def column_args
  @columns.map { |col| "#{col.name}:#{col.sql_type}" unless col.name == "id" }.compact
end

#generate_file(type) ⇒ Object

rubocop:disable Metrics/MethodLength



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/domino/scaffolder.rb', line 48

def generate_file(type) # rubocop:disable Metrics/MethodLength
  require "erb"

  template_path = File.expand_path("../generators/domino/templates/#{type}.rb.tt", __FILE__)
  raise "Missing template: #{template_path}" unless File.exist?(template_path)

  content = ERB.new(File.read(template_path)).result(binding)

  puts "Generating #{type} for #{@model_name}"

  folder = case type
           when "blueprint" then "app/mappers"
           when "controller" then "app/controllers"
           when "repository" then "app/repositories"
           when "service" then "app/services"
           else "app/#{type}s"
           end

  filename = case type
             when "controller"
               "#{@model_name.underscore.pluralize}_controller.rb"
             else
               "#{@model_name.underscore}_#{type}.rb"
             end

  FileUtils.mkdir_p(folder)
  File.write(File.join(folder, filename), content)
end

#generate_model_fileObject



40
41
42
# File 'lib/domino/scaffolder.rb', line 40

def generate_model_file
  system("rails generate model #{@model_name} #{column_args.join(" ")}")
end

#runObject



32
33
34
35
36
37
38
# File 'lib/domino/scaffolder.rb', line 32

def run
  generate_model_file if @generate_model
  generate_file("repository")
  generate_file("service")
  generate_file("blueprint")
  generate_file("controller")
end