class Rammer::RammerGenerator

Generator class for creating application basic folder structure

Attributes

gem_path[RW]
module_name[RW]
project_name[RW]
target_dir[RW]
valid_name[RW]

Public Class Methods

new(dir_name) click to toggle source

Initiliazes the following attributes :

project_name (application name), valid_name (boolean value for validation), target_dir (new application path)
and gem_path (path at which the gem is installed)
# File lib/rammer/rammer_generator.rb, line 47
def initialize(dir_name)
  @project_name   = dir_name
  @valid_name = true

  if RESERVED_WORDS.include? @project_name
    $stdout.puts "\e[1;31mError:\e[0m Invalid application name #{@project_name}. Please give a name which does not match one of the reserved rammer words."
    @valid_name = false
  end
  
  @target_dir  =  Dir.pwd + "/" + @project_name
  path = `gem which rammer`
        @gem_path = path.split($gem_file_name,2).first + $gem_file_name        
end

Public Instance Methods

run() click to toggle source

Creates a basic folder structure with required files and configuration setup.

# File lib/rammer/rammer_generator.rb, line 64
def run
  unless !@valid_name || File.exists?(@project_name) || File.directory?(@project_name)
    $stdout.puts "Creating goliath application under the directory #{@project_name}"
    FileUtils.mkdir @project_name
    
    create_base_dirs
    copy_files_to_target
    setup_api_module
    copy_files_to_dir 'application.rb','config'
    copy_files_to_dir 'database.yml','config'
    $stdout.puts "\e[1;32m \trun\e[0m\tbundle install"
    Dir.chdir("#{@project_name}")
    system("bundle install")
  else 
    unless !@valid_name
      $stdout.puts "\e[1;31mError:\e[0m The directory #{@project_name} already exists, aborting. Maybe move it out of the way before continuing."
    end
  end
end

Private Instance Methods

config_server() click to toggle source

Function to configure the Goliath server.

# File lib/rammer/rammer_generator.rb, line 122
def config_server
  file = File.open("#{@project_name}/server.rb", "r+")
  file.each do |line|          
    while line == "  def response(env)\n" do
      pos = file.pos
      rest = file.read
      file.seek pos
      file.write("\t::") 
      file.write(@module_name)
      file.write("::Base.call(env)\n")
      file.write(rest)
      $stdout.puts "\e[1;35m \tconfig\e[0m\tserver.rb"
      return
    end
  end
end
copy_files_to_dir(file,destination) click to toggle source

Creates api modules, required files and configures the server with respect to new application.

# File lib/rammer/rammer_generator.rb, line 153
def copy_files_to_dir(file,destination)
  FileUtils.cp("#{@gem_path}/lib/modules/common/#{file}","#{@project_name}/#{destination}")
  $stdout.puts "\e[1;32m \tcreate\e[0m\t#{destination}/#{file}"
end
copy_files_to_target() click to toggle source

Function to copy the template files project location.

# File lib/rammer/rammer_generator.rb, line 142
def copy_files_to_target
  COMMON_RAMMER_FILES.each do |file|
    source = File.join("#{@gem_path}/lib/modules/common/",file)
    FileUtils.cp(source,"#{@project_name}")
    $stdout.puts "\e[1;32m \tcreate\e[0m\t#{file}"
  end
end
create_api_module() click to toggle source

Function to create the API modules.

# File lib/rammer/rammer_generator.rb, line 110
def create_api_module
  File.open("#{@project_name}/app/apis/#{@project_name}/base.rb", "w") do |f|
    f.write('module ') 
    f.puts(@module_name)
    f.write("\tclass Base < Grape::API\n\tend\nend")
  end
  $stdout.puts "\e[1;32m \tcreate\e[0m\tapp/apis/#{@project_name}/base.rb"
end
create_base_dirs() click to toggle source

Creates the application base directories.

# File lib/rammer/rammer_generator.rb, line 89
def create_base_dirs
  BASE_DIR.each do |dir|
    FileUtils.mkdir "#{@project_name}/#{dir}"
    $stdout.puts "\e[1;32m \tcreate\e[0m\t#{dir}"
  end
  FileUtils.mkdir "#{@project_name}/app/apis/#{@project_name}"
  $stdout.puts "\e[1;32m \tcreate\e[0m\tapp/apis/#{@project_name}"
end
setup_api_module() click to toggle source

Function to setup the API modules.

# File lib/rammer/rammer_generator.rb, line 101
def setup_api_module
  @module_name = @project_name.split('_').map(&:capitalize).join('')
  create_api_module
  config_server
end