lib/lotus/generators/application/app.rb in lotusrb-0.4.1 vs lib/lotus/generators/application/app.rb in lotusrb-0.5.0

- old
+ new

@@ -1,38 +1,39 @@ require 'shellwords' require 'lotus/generators/abstract' +require 'lotus/generators/database_config' module Lotus module Generators module Application class App < Abstract def initialize(command) super - @upcase_app_name = app_name.to_env_s - @classified_app_name = Utils::String.new(app_name).classify - @lotus_head = options.fetch(:lotus_head) - @test = options[:test] - @database = options[:database] - @application_base_url = options[:application_base_url] - @lotus_model_version = '~> 0.4' + @upcase_app_name = app_name.to_env_s + @classified_app_name = Utils::String.new(app_name).classify + @lotus_head = options.fetch(:lotus_head) + @test = options[:test] + @database_config = DatabaseConfig.new(options[:database], app_name) + @application_base_url = options[:application_base_url] + @lotus_model_version = '~> 0.5' cli.class.source_root(source) end def start opts = { - app_name: app_name, - upcase_app_name: @upcase_app_name, - classified_app_name: @classified_app_name, - application_base_url: @application_base_url, - lotus_head: @lotus_head, - test: @test, - database: @database, - database_config: database_config, - lotus_model_version: @lotus_model_version, + app_name: app_name, + upcase_app_name: @upcase_app_name, + classified_app_name: @classified_app_name, + application_base_url: @application_base_url, + lotus_head: @lotus_head, + test: @test, + database: @database_config.engine, + database_config: @database_config.to_hash, + lotus_model_version: @lotus_model_version, } templates = { 'lotusrc.tt' => '.lotusrc', '.env.tt' => '.env', @@ -52,15 +53,16 @@ empty_directories = [ "app/controllers", "app/views", "lib/#{ app_name }/entities", "lib/#{ app_name }/repositories", + "lib/#{ app_name }/mailers", "public/javascripts", "public/stylesheets" ] - empty_directories << if sql_database? + empty_directories << if @database_config.sql? "db/migrations" else "db" end @@ -89,14 +91,15 @@ end empty_directories << [ "spec/#{ app_name }/entities", "spec/#{ app_name }/repositories", + "spec/#{ app_name }/mailers", "spec/support" ] - if sql_database? + if @database_config.sql? templates.merge!( 'schema.sql.tt' => 'db/schema.sql' ) end @@ -108,78 +111,18 @@ gitkeep = '.gitkeep' cli.template(source.join(gitkeep), target.join(dir, gitkeep), opts) end unless git_dir_present? - cli.template(source.join(database_type == :file_system ? 'gitignore.tt' : '.gitignore'), target.join('.gitignore'), opts) + cli.template(source.join(@database_config.type == :file_system ? 'gitignore.tt' : '.gitignore'), target.join('.gitignore'), opts) cli.run("git init #{Shellwords.escape(target)}", capture: true) end end private def git_dir_present? File.directory?(source.join('.git')) - end - - def database_config - { - gem: database_gem, - uri: database_uri, - type: database_type - } - end - - def database_gem - { - 'mysql' => 'mysql', - 'mysql2' => 'mysql2', - 'postgresql' => 'pg', - 'postgres' => 'pg', - 'sqlite' => 'sqlite3', - 'sqlite3' => 'sqlite3' - }[@database] - end - - def sql_database? - database_type == :sql - end - - def database_type - case @database - when 'mysql', 'mysql2', 'postgresql', 'postgres', 'sqlite', 'sqlite3' - :sql - when 'filesystem' - :file_system - when 'memory' - :memory - end - end - - def database_uri - { - development: "#{database_base_uri}_development", - test: "#{database_base_uri}_test" - } - end - - def database_base_uri - case @database - when 'mysql' - "mysql://localhost/#{app_name}" - when 'mysql2' - "mysql2://localhost/#{app_name}" - when 'postgresql', 'postgres' - "postgres://localhost/#{app_name}" - when 'sqlite', 'sqlite3' - "sqlite://db/#{Shellwords.escape(app_name)}" - when 'memory' - "memory://localhost/#{app_name}" - when 'filesystem' - "file:///db/#{app_name}" - else - raise "\"#{@database}\" is not a valid database type" - end end end end end end