Class: Radical::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/radical/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, props) ⇒ Generator

Returns a new instance of Generator.



8
9
10
11
# File 'lib/radical/generator.rb', line 8

def initialize(name, props)
  @name = name
  @props = props
end

Instance Method Details

#appObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/radical/generator.rb', line 61

def app
  @name = nil if @name == '.'
  parts = [Dir.pwd, @name].compact
  dir = File.join(*parts)
  FileUtils.mkdir_p dir

  %w[
    assets/css
    assets/js
    controllers
    migrations
    models
    views
  ].each do |dir_|
    puts "Creating directory #{dir_}"
    FileUtils.mkdir_p File.join(dir, dir_)
  end

  Dir[File.join(__dir__, 'generator', 'app', '**', '*.*')].sort.each do |template|
    contents = File.read(template)
    filename = File.join(dir, File.path(template).gsub("#{__dir__}/generator/app/", ''))

    write(filename, contents)
  end

  # Explicitly include .env
  template = File.join(__dir__, 'generator', 'app', '.env')
  contents = instance_eval File.read(template)
  filename = File.join(dir, '.env')
  write(filename, contents)
end

#controllerObject



40
41
42
43
44
45
46
47
# File 'lib/radical/generator.rb', line 40

def controller
  template = instance_eval File.read File.join(__dir__, 'generator', 'controller.rb')
  dir = File.join(Dir.pwd, 'controllers')
  FileUtils.mkdir_p dir
  filename = File.join(dir, "#{plural}.rb")

  write(filename, template)
end

#migration(model: true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/radical/generator.rb', line 20

def migration(model: true)
  dir = File.join(Dir.pwd, 'migrations')
  FileUtils.mkdir_p dir

  template = instance_eval File.read(File.join(__dir__, 'generator', "#{model ? '' : 'blank_'}migration.rb"))
  migration_name = model ? "#{Time.now.to_i}_create_table_#{plural}.rb" : "#{Time.now.to_i}_#{@name}.rb"
  filename = File.join(dir, migration_name)

  write(filename, template)
end

#modelObject



31
32
33
34
35
36
37
38
# File 'lib/radical/generator.rb', line 31

def model
  template = instance_eval File.read File.join(__dir__, 'generator', 'model.rb')
  dir = File.join(Dir.pwd, 'models')
  FileUtils.mkdir_p dir
  filename = File.join(dir, "#{singular}.rb")

  write(filename, template)
end

#mvcObject



13
14
15
16
17
18
# File 'lib/radical/generator.rb', line 13

def mvc
  migration
  model
  views
  controller
end

#viewsObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/radical/generator.rb', line 49

def views
  dir = File.join(Dir.pwd, 'views', plural)
  FileUtils.mkdir_p dir

  Dir[File.join(__dir__, 'generator', 'views', '*.rb')].sort.each do |template|
    contents = instance_eval File.read template
    filename = File.join(dir, "#{File.basename(template, '.rb')}.erb")

    write(filename, contents)
  end
end