Skip to content

Commit

Permalink
Refator database configuration for generators
Browse files Browse the repository at this point in the history
- Extract common behaviour between generators (app, container)
- Add unit specs covering database config logic
- Add multi-platform support (mri, jruby)
- Fix CLI specs setup
  • Loading branch information
brennovich committed Aug 23, 2015
1 parent e23cb3c commit 0dd9559
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 173 deletions.
99 changes: 20 additions & 79 deletions lib/lotus/generators/application/app.rb
@@ -1,5 +1,6 @@
require 'shellwords'
require 'lotus/generators/abstract'
require 'lotus/generators/database_config'

module Lotus
module Generators
Expand All @@ -8,29 +9,29 @@ 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.4'

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 = {
Expand Down Expand Up @@ -58,7 +59,7 @@ def start
"public/stylesheets"
]

empty_directories << if sql_database?
empty_directories << if @database_config.sql?
"db/migrations"
else
"db"
Expand Down Expand Up @@ -94,7 +95,7 @@ def start
"spec/support"
]

if sql_database?
if @database_config.sql?
templates.merge!(
'schema.sql.tt' => 'db/schema.sql'
)
Expand All @@ -110,7 +111,7 @@ def start
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
Expand All @@ -120,66 +121,6 @@ def start
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
Expand Down
98 changes: 15 additions & 83 deletions lib/lotus/generators/application/container.rb
@@ -1,5 +1,6 @@
require 'shellwords'
require 'lotus/generators/abstract'
require 'lotus/generators/database_config'
require 'lotus/generators/slice'

module Lotus
Expand All @@ -9,23 +10,23 @@ class Container < Abstract
def initialize(command)
super

@slice_generator = Slice.new(command)
@lotus_head = options.fetch(:lotus_head)
@test = options[:test]
@database = options[:database]
@lotus_model_version = '~> 0.4'
@slice_generator = Slice.new(command)
@database_config = DatabaseConfig.new(options[:database], app_name)
@lotus_head = options.fetch(:lotus_head)
@test = options[:test]
@lotus_model_version = '~> 0.4'

cli.class.source_root(source)
end

def start
opts = {
app_name: app_name,
lotus_head: @lotus_head,
test: @test,
database: @database,
database_config: database_config,
lotus_model_version: @lotus_model_version,
app_name: app_name,
lotus_head: @lotus_head,
test: @test,
database: @database_config.engine,
database_config: @database_config.to_hash,
lotus_model_version: @lotus_model_version,
}

templates = {
Expand All @@ -45,7 +46,7 @@ def start
"lib/#{ app_name }/repositories"
]

empty_directories << if sql_database?
empty_directories << if @database_config.sql?
"db/migrations"
else
"db"
Expand All @@ -68,7 +69,7 @@ def start
)
end

if sql_database?
if @database_config.sql?
templates.merge!(
'schema.sql.tt' => 'db/schema.sql'
)
Expand All @@ -90,7 +91,7 @@ def start
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

Expand All @@ -102,75 +103,6 @@ def start
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 database_type
case @database
when 'mysql', 'mysql2', 'postgresql', 'postgres', 'sqlite', 'sqlite3'
:sql
when 'filesystem'
:file_system
when 'memory'
:memory
end
end

def sql_database?
database_type == :sql
end

def database_uri
{
development: database_environment_uri(:development),
test: database_environment_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

def database_environment_uri(environment)
case @database
when 'sqlite', 'sqlite3'
"#{database_base_uri}_#{environment}.sqlite"
else
"#{database_base_uri}_#{environment}"
end
end
end
end
end
Expand Down
88 changes: 88 additions & 0 deletions lib/lotus/generators/database_config.rb
@@ -0,0 +1,88 @@
module Lotus
module Generators
class DatabaseConfig
SUPPORTED_ENGINES = {
'mysql' => { type: :sql, mri: 'mysql', jruby: 'jdbc-mysql' },
'mysql2' => { type: :sql, mri: 'mysql2', jruby: 'jdbc-mysql' },
'postgresql' => { type: :sql, mri: 'pg', jruby: 'jdbc-postgres' },
'postgres' => { type: :sql, mri: 'pg', jruby: 'jdbc-postgres' },
'sqlite' => { type: :sql, mri: 'sqlite3', jruby: 'jdbc-sqlite3' },
'sqlite3' => { type: :sql, mri: 'sqlite3', jruby: 'jdbc-sqlite3' },
'filesystem' => { type: :file_system, mri: nil, jruby: nil },
'memory' => { type: :memory, mri: nil, jruby: nil }
}.freeze

attr_reader :engine, :name

def initialize(engine, name)
@engine = engine
@name = name

SUPPORTED_ENGINES.key?(engine) or fail "\"#{ engine }\" is not a valid database type"
end

def to_hash
{
gem: gem,
uri: uri,
type: type
}
end

def type
SUPPORTED_ENGINES[engine][:type]
end

def sql?
type == :sql
end

private

def platform
Lotus::Utils.jruby? ? :jruby : :mri
end

def platform_prefix
:jdbc if Lotus::Utils.jruby?
end

def uri
{
development: environment_uri(:development),
test: environment_uri(:test)
}
end

def gem
SUPPORTED_ENGINES[engine][platform]
end

def base_uri
case engine
when 'mysql'
"mysql://localhost/#{ name }"
when 'mysql2'
"mysql2://localhost/#{ name }"
when 'postgresql', 'postgres'
"postgres://localhost/#{ name }"
when 'sqlite', 'sqlite3'
"sqlite://db/#{ Shellwords.escape(name) }"
when 'memory'
"memory://localhost/#{ name }"
when 'filesystem'
"file:///db/#{ Shellwords.escape(name) }"
end
end

def environment_uri(environment)
case engine
when 'sqlite', 'sqlite3'
"#{ platform_prefix }#{ base_uri }_#{ environment }.sqlite"
else
"#{ platform_prefix }#{ base_uri }_#{ environment }"
end
end
end
end
end

0 comments on commit 0dd9559

Please sign in to comment.