class Radical::Generator

Public Class Methods

new(name, props) click to toggle source
# File lib/radical/generator.rb, line 8
def initialize(name, props)
  @name = name
  @props = props
end

Public Instance Methods

app() click to toggle source
# 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
controller() click to toggle source
# 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) click to toggle source
# 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
model() click to toggle source
# 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
mvc() click to toggle source
# File lib/radical/generator.rb, line 13
def mvc
  migration
  model
  views
  controller
end
views() click to toggle source
# 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

Private Instance Methods

columns(leading:) click to toggle source
# File lib/radical/generator.rb, line 120
def columns(leading:)
  @props
    .map { |p| p.split(':') }
    .map { |name, type| "t.#{type} #{name}" }
    .join "#{' ' * leading}\n"
end
inputs(leading:) click to toggle source
# File lib/radical/generator.rb, line 141
def inputs(leading:)
  @props
    .map { |p| p.split(':').first }
    .map { |name| "<%== f.text :#{name} %>" }
    .join "#{' ' * leading}\n"
end
params() click to toggle source
# File lib/radical/generator.rb, line 148
def params
  @props
    .map { |p| p.split(':').first }
    .map { |name| "'#{name}'" }
    .join ', '
end
plural() click to toggle source
# File lib/radical/generator.rb, line 116
def plural
  Strings.camel_case plural_constant
end
plural_constant() click to toggle source
# File lib/radical/generator.rb, line 108
def plural_constant
  @name.gsub(/[)(]/, '')
end
singular() click to toggle source
# File lib/radical/generator.rb, line 112
def singular
  Strings.camel_case singular_constant
end
singular_constant() click to toggle source
# File lib/radical/generator.rb, line 104
def singular_constant
  @name.gsub(/\(.*\)/, '')
end
td(leading:) click to toggle source
# File lib/radical/generator.rb, line 134
def td(leading:)
  @props
    .map { |p| p.split(':').first }
    .map { |name| "<td><%= #{singular}.#{name} %></td>" }
    .join "#{' ' * leading}\n"
end
th(leading:) click to toggle source
# File lib/radical/generator.rb, line 127
def th(leading:)
  @props
    .map { |p| p.split(':').first }
    .map { |name| "<th>#{name}</th>" }
    .join "#{' ' * leading}\n"
end
write(filename, contents) click to toggle source
# File lib/radical/generator.rb, line 95
def write(filename, contents)
  if File.exist?(filename)
    puts "Skipped #{File.basename(filename)}"
  else
    File.write(filename, contents)
    puts "Created #{filename}"
  end
end