Permalink
Browse files

Turn `App::Sqitch->engine` into an object.

The engine name is now stored in `_engine`. Other interfaces should just assume the should use the engine object when they need it. It will be lazily-loaded then it is needed.
  • Loading branch information...
theory committed Apr 26, 2012
1 parent 07672ee commit 2de1b99b41b4f97ddd8a865663307facee164cd8
Showing with 30 additions and 9 deletions.
  1. +10 −4 lib/App/Sqitch.pm
  2. +12 −1 lib/App/Sqitch/Command/init.pm
  3. +1 −1 lib/App/Sqitch/Engine.pm
  4. +6 −2 t/base.t
  5. +1 −1 t/init.t
View
@@ -20,7 +20,13 @@ has plan_file => (is => 'ro', required => 1, default => sub {
file 'sqitch.plan';
});
-has engine => (is => 'ro', isa => enum [qw(pg mysql sqlite)]);
+has _engine => (is => 'ro', isa => enum [qw(pg mysql sqlite)]);
+has engine => (is => 'ro', isa => 'Maybe[App::Sqitch::Engine]', lazy => 1, default => sub {
+ my $self = shift;
+ my $name = $self->_engine or return;
+ require App::Sqitch::Engine;
+ App::Sqitch::Engine->load({sqitch => $self, engine => $name});
+});
has client => (is => 'ro', isa => 'Str');
@@ -83,9 +89,9 @@ sub go {
my $config = App::Sqitch::Config->new;
# 4. Instantiate Sqitch.
- my $sqitch = $class->new(
- merge $core_opts, $config->get_section(section => 'core')
- );
+ my $params = merge $core_opts, $config->get_section(section => 'core');
+ $params->{_engine} = delete $params->{engine};
+ my $sqitch = $class->new($params);
$sqitch->{config} = $config;
# 5. Instantiate the command object.
@@ -54,7 +54,6 @@ sub write_config {
revert_dir
test_dir
extension
- engine
)) {
# Set core attributes that are not their default values and not
# already in user or system config.
@@ -71,6 +70,18 @@ sub write_config {
}
# XXX Add core.$engine section.
+ ENGINE: {
+ if (my $engine = $sqitch->engine) {
+ if (my $ce = $config->get(key => 'core.engine')) {
+ last ENGINE if $ce eq $engine->name;
+ }
+ $config->set(
+ key => "core.engine",
+ value => $engine->name,
+ filename => $file,
+ );
+ }
+ }
$self->info("Created $file");
return $self;
View
@@ -75,7 +75,7 @@ Instantiates and returns a App::Sqitch::Engine object.
=head2 Instance Methods
-=head3 C<engine>
+=head3 C<name>
my $name = $engine->name;
View
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use Test::More tests => 32;
+use Test::More tests => 33;
#use Test::More 'no_plan';
use Test::MockModule;
use Path::Class;
@@ -18,6 +18,7 @@ can_ok $CLASS, qw(
new
plan_file
engine
+ _engine
client
db_name
username
@@ -37,6 +38,7 @@ can_ok $CLASS, qw(
isa_ok my $sqitch = $CLASS->new, $CLASS, 'A new object';
for my $attr (qw(
+ _engine
engine
client
host
@@ -72,7 +74,9 @@ GO: {
is_deeply \@params, ['config'], 'Extra args should be passed to execute';
isa_ok my $sqitch = $cmd->sqitch, 'App::Sqitch';
- is $sqitch->engine, 'sqlite', 'Engine should be set by option';
+ is $sqitch->_engine, 'sqlite', 'Engine should be set by option';
+ # isa $sqitch->engine, 'App::Sqitch::Engine::sqlite',
+ # 'Engine object should be constructable';
is $sqitch->db_name, 'widgetopolis', 'db_name should be set by config';
is $sqitch->extension, 'ddl', 'ddl should be set by config';
ok my $config = $sqitch->config, 'Get the Sqitch config';
View
@@ -147,7 +147,7 @@ $sqitch = App::Sqitch->new(
revert_dir => 'rev',
test_dir => 'tst',
extension => 'ddl',
- engine => 'sqlite',
+ _engine => 'sqlite',
);
ok $init = $CLASS->new(sqitch => $sqitch),

0 comments on commit 2de1b99

Please sign in to comment.