lib/lotus/generators/database_config.rb in lotusrb-0.5.0 vs lib/lotus/generators/database_config.rb in lotusrb-0.6.0
- old
+ new
@@ -1,8 +1,11 @@
+require 'shellwords'
+
module Lotus
module Generators
class DatabaseConfig
+
SUPPORTED_ENGINES = {
'mysql' => { type: :sql, mri: 'mysql2', 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' },
@@ -10,17 +13,19 @@
'sqlite3' => { type: :sql, mri: 'sqlite3', jruby: 'jdbc-sqlite3' },
'filesystem' => { type: :file_system, mri: nil, jruby: nil },
'memory' => { type: :memory, mri: nil, jruby: nil }
}.freeze
+ DEFAULT_ENGINE = 'filesystem'.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"
+ SUPPORTED_ENGINES.key?(engine.to_s) or fail "\"#{ engine }\" is not a valid database type"
end
def to_hash
{
gem: gem,
@@ -35,18 +40,22 @@
def sql?
type == :sql
end
+ def filesystem?
+ type == :file_system
+ end
+
private
def platform
Lotus::Utils.jruby? ? :jruby : :mri
end
def platform_prefix
- :jdbc if Lotus::Utils.jruby?
+ 'jdbc:'.freeze if Lotus::Utils.jruby?
end
def uri
{
development: environment_uri(:development),
@@ -59,11 +68,15 @@
end
def base_uri
case engine
when 'mysql', 'mysql2'
- "mysql2://localhost/#{ name }"
+ if Lotus::Utils.jruby?
+ "mysql://localhost/#{ name }"
+ else
+ "mysql2://localhost/#{ name }"
+ end
when 'postgresql', 'postgres'
"postgres://localhost/#{ name }"
when 'sqlite', 'sqlite3'
"sqlite://db/#{ Shellwords.escape(name) }"
when 'memory'
@@ -76,10 +89,10 @@
def environment_uri(environment)
case engine
when 'sqlite', 'sqlite3'
"#{ platform_prefix }#{ base_uri }_#{ environment }.sqlite"
else
- "#{ platform_prefix }#{ base_uri }_#{ environment }"
+ "#{ platform_prefix if sql? }#{ base_uri }_#{ environment }"
end
end
end
end
end