Skip to content

Commit

Permalink
convert roles to Moo::Role
Browse files Browse the repository at this point in the history
  • Loading branch information
felliott committed Nov 3, 2014
1 parent e416d30 commit 93aca80
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 37 deletions.
1 change: 0 additions & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ requires 'Digest::MD5' => '2.39';
requires 'Hash::Merge' => '0.11';
requires 'List::MoreUtils' => '0.22';
requires 'Module::Runtime' => '0.013';
requires 'Moose' => '1.10';
requires 'Moo' => '1.006';
requires 'namespace::clean';
requires 'Path::Class' => '0.21';
Expand Down
4 changes: 2 additions & 2 deletions lib/Test/DBIx/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ to an already existing and populated database, set this option to false.
=item traits
Traits are L<Moose::Role>s that are applied to the class managing the connection
Traits are L<Moo::Role>s that are applied to the class managing the connection
to your database. If you leave this option blank and you don't specify anything
for 'connect_info' (above), we automatically load the SQLite trait (which can
be reviewed at L<Test::DBIx::Class::SchemaManager::Trait::SQLite>). This trait
Expand Down Expand Up @@ -1387,7 +1387,7 @@ directory.
=head1 TRAITS
As described, a trait is a L<Moose::Role> that is applied to the class
As described, a trait is a L<Moo::Role> that is applied to the class
managing your database and test instance. Traits are installed by the
'traits' configuration option, which expects an ArrayRef as its input
(however will also normalize a scalar to an ArrayRef).
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/DBIx/Class/Role/FixtureCommand.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Test::DBIx::Class::Role::FixtureCommand; {

use Moose::Role;
use Moo::Role;
requires qw/install_fixtures/;

has 'schema_manager' => (
Expand Down
4 changes: 2 additions & 2 deletions lib/Test/DBIx/Class/SchemaManager.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package Test::DBIx::Class::SchemaManager;

use Moose::Util;
use Test::More ();
use List::MoreUtils qw(uniq);
use Moo::Role ();
use Test::DBIx::Class::Types qw( :types :to );
use Types::Standard qw(Bool HashRef Str);

Expand Down Expand Up @@ -197,7 +197,7 @@ sub initialize_schema {
@traits = map { __PACKAGE__."::Trait::$_"} uniq @traits;
$config->{traits} = \@traits;

my $self = Moose::Util::with_traits($class, @traits)->new($config)
my $self = Moo::Role->create_class_with_roles($class, @traits)->new($config)
or return;

$self->schema->storage->ensure_connected;
Expand Down
5 changes: 3 additions & 2 deletions lib/Test/DBIx/Class/SchemaManager/Trait/SQLite.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package Test::DBIx::Class::SchemaManager::Trait::SQLite; {

use Moose::Role;

use Test::DBIx::Class::Types qw(ConnectInfo);

use Moo::Role;

sub dbname {
my ($self) = @_;

Expand Down
95 changes: 74 additions & 21 deletions lib/Test/DBIx/Class/SchemaManager/Trait/Testmysqld.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package Test::DBIx::Class::SchemaManager::Trait::Testmysqld;

use Moose::Role;
use Test::mysqld;
use Test::More ();
use Path::Class qw(dir);
Expand All @@ -10,21 +9,24 @@ use File::Temp qw(tempdir);
use File::Path qw(make_path);
use Types::Standard qw(ArrayRef HashRef Str);

use Moo::Role;

requires 'setup', 'cleanup';

## has '+force_drop_table' => (is=>'rw',default=>1);

has base_dir => (is => 'ro', builder => '_build_base_dir');
has base_dir => (is => 'ro', builder => 1,);
sub _build_base_dir { $ENV{base_dir} || $ENV{BASE_DIR} }
has mysql_install_db => (is => 'ro', builder => '_build_mysql_install_db');
has mysql_install_db => (is => 'ro', builder => 1,);
sub _build_mysql_install_db { $ENV{mysql_install_db} || $ENV{MYSQL_INSTALL_DB} }
has mysqld => (is => 'ro', builder => '_build_mysqld');
has mysqld => (is => 'ro', builder => 1,);
sub _build_mysqld { $ENV{mysqld} || $ENV{MYSQLD} }

has test_db_manager => (
is=>'ro',
init_arg=>undef,
lazy_build=>1,
lazy=>1,
builder=>1,
);

sub _build_test_db_manager {
Expand All @@ -42,15 +44,14 @@ sub _build_test_db_manager {
}
}

has default_cnf => (
has _default_cnf => (
is=>'ro',
init_arg=>undef,
isa=>HashRef,
auto_deref=>1,
lazy_build=>1,
lazy=>1,
builder=>1,
);

sub _build_default_cnf {
sub _build__default_cnf {
my $port = $_[0]->find_next_unused_port();
return {
'server-id'=>1,
Expand All @@ -59,29 +60,67 @@ sub _build_default_cnf {
'port'=>$port,
};
}
sub default_cnf { wantarray ? %{$_[0]->_default_cnf} : $_[0]->_default_cnf }


has port_to_try_first => (
is=>'rw',
default=> sub { 8000 + int(rand(2000)) },
is =>'rw',
builder => 1,
);
sub _build_port_to_try_first { 8000 + int(rand(2000)) }

has my_cnf => (
has _my_cnf => (
is=>'ro',
isa=>HashRef,
auto_deref=>1,
init_arg=>'my_cnf',
);
sub my_cnf {
my $self = shift;
if (not defined $self->{_my_cnf}) {
return wantarray ? () : undef;
}
return wantarray ? %{$self->_my_cnf} : $self->_my_cnf;
}

## Replicant stuff... probably should be a delegate

has deployed_replicants => (is=>'rw', isa=>ArrayRef, auto_deref=>1);
has _deployed_replicants => (
is=>'rw',
isa=>ArrayRef,
init_arg=>'deployed_replicants',
);
sub deployed_replicants {
my $self = shift;

return $self->_deployed_replicants(@_) if (@_);

if (not defined $self->{_deployed_replicants}) {
return wantarray ? () : undef;
}

return wantarray ? @{$self->_deployed_replicants}
: $self->_deployed_replicants;
}

has replicants => (
has _replicants => (
is=>'rw',
isa=>ReplicantsConnectInfo,
coerce=>1,
auto_deref=>1,
init_arg=>'replicants',
predicate=>'has_replicants',
);
sub replicants {
my $self = shift;

my $ret = $self->_replicants;
return $self->_replicants(@_) if (@_);

if (not defined $ret) {
return wantarray ? () : undef;
}

return wantarray ? @$ret : $ret;
}

has pool_args => (
is=>'ro',
Expand Down Expand Up @@ -111,20 +150,34 @@ has balancer_args => (
},
);

has default_replicant_cnf => (
has _default_replicant_cnf => (
is=>'ro',
init_arg=>undef,
isa=>HashRef,
auto_deref=>1,
init_arg=>'default_replicant_cnf',
required=>1,
default=> sub { +{} },
);
sub default_replicant_cnf {
my $self = shift;
if (not defined $self->{_default_replicant_cnf}) {
return wantarray ? () : undef;
}
return wantarray ? %{$self->_default_replicant_cnf} : $self->_default_replicant_cnf;
}

has my_replicant_cnf => (
has _my_replicant_cnf => (
is=>'ro',
isa=>HashRef,
auto_deref=>1,
init_arg=>'my_replicant_cnf',
);
sub my_replicant_cnf {
my $self = shift;
if (not defined $self->{_my_replicant_cnf}) {
return wantarray ? () : undef;
}
return wantarray ? %{$self->_my_replicant_cnf} : $self->_my_replicant_cnf;
}

sub prepare_replicant_config {
my ($self, $replicant, @replicants,%extra) = @_;
Expand Down
18 changes: 10 additions & 8 deletions lib/Test/DBIx/Class/SchemaManager/Trait/Testpostgresql.pm
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package Test::DBIx::Class::SchemaManager::Trait::Testpostgresql; {

use Moose::Role;

use Test::PostgreSQL;
use Test::More ();
use Path::Class qw(dir);

use Moo::Role;


has postgresqlobj => (
is=>'ro',
init_arg=>undef,
lazy_build=>1,
lazy=>1,
builder=>1,
);

has base_dir => ( is => 'ro', builder => '_build_base_dir' );
has base_dir => ( is => 'ro', builder => 1 );
sub _build_base_dir { $ENV{base_dir} || $ENV{BASE_DIR} };

has initdb => ( is => 'ro', builder => '_build_initdb' );
has initdb => ( is => 'ro', builder => 1 );
sub _build_initdb { $ENV{initdb} || $ENV{INITDB} };

has postmaster => ( is => 'ro', builder => '_build_postmaster' );
has postmaster => ( is => 'ro', builder => 1 );
sub _build_postmaster { $ENV{postmaster} || $ENV{POSTMASTER} };

sub _build_postgresqlobj {
Expand Down Expand Up @@ -75,11 +77,11 @@ package Test::DBIx::Class::SchemaManager::Trait::Testpostgresql; {
}
};

override drop_table_sql => sub {
sub drop_table_sql {
my $self = shift;
my $table = shift;
return "drop table $table cascade";
};
}

} 1;

Expand Down

0 comments on commit 93aca80

Please sign in to comment.