Skip to content

Commit

Permalink
v0.991
Browse files Browse the repository at this point in the history
  • Loading branch information
kberov committed Oct 10, 2012
1 parent c152739 commit 644789a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 29 deletions.
9 changes: 8 additions & 1 deletion Changes
@@ -1,9 +1,16 @@
Revision history for Mojolicious::Plugin::DSC

0.991 2012-10-11 01:43:05 EEST
- Fixed register(). Now using $app->$dbix_helper - not $app->dbix.
- Fixed POD so Test::Pod::Coverage does not report "config" as naked.
- Increased DBIx::Simple::Class verion dependency.
- Hidden from CPAN example classes used for testing.
- Jumping from 0.60 to be inline with DBIx::Simple::Class.

0.60 2012-08-09 22:48 EEST
- Jumping from 0.06 to 0.60 to be closer to DBIx::Simple::Class.
- Multiple dbix helpers. My->dbix can be different from Your->dbix if overriden. See DBIx::Simple::Class/dbix
- Multiple dbix helpers. My->dbix can be different from Your->dbix
if overriden. See DBIx::Simple::Class/dbix
- Try to guess default namespace for loading classes
from $config->{database} and $config->{dsn}.
Supported drivers:mysql, SQLite, Pg.
Expand Down
2 changes: 1 addition & 1 deletion Makefile.PL
Expand Up @@ -9,7 +9,7 @@ WriteMakefile(
ABSTRACT_FROM => 'lib/Mojolicious/Plugin/DSC.pm',
AUTHOR => q{Красимир Беров <berov@cpan.org>},
LICENSE => 'artistic_2',
PREREQ_PM => {'Mojolicious' => '2.0', 'DBIx::Simple::Class' => '0.62'},
PREREQ_PM => {'Mojolicious' => '2.0', 'DBIx::Simple::Class' => '0.991'},
META_MERGE => {
requires => {perl => '5.010001'},
resources => {
Expand Down
27 changes: 13 additions & 14 deletions lib/Mojolicious/Plugin/DSC.pm
Expand Up @@ -5,12 +5,12 @@ use Mojo::Util qw(camelize);
use Carp;


our $VERSION = '0.60';
our $VERSION = '0.991';

#some known good defaults
my $COMMON_ATTRIBUTES = {
RaiseError => 1,
AutoCommit => 1,
RaiseError => 1,
AutoCommit => 1,
};


Expand Down Expand Up @@ -60,16 +60,12 @@ sub register {
. 'must be an ARRAY reference containing a list of classes to load.')
unless (ref($config->{load_classes}) eq 'ARRAY');

DBIx::Simple::Class->DEBUG($config->{DEBUG});

#ready... Go!
my $dbix = DBIx::Simple->connect(
$config->{dsn},
$config->{user} || '',
$config->{password} || '',
{ %$COMMON_ATTRIBUTES,
%{$config->{dbh_attributes}}
}
{%$COMMON_ATTRIBUTES, %{$config->{dbh_attributes}}}
);
$config->{onconnect_do} ||= [];
if (!ref($config->{onconnect_do})) {
Expand All @@ -79,12 +75,15 @@ sub register {
$dbix->dbh->do($sql);
}

$config->{dbix_helper} ||= 'dbix';
$app->attr($config->{dbix_helper}, sub {$dbix});
$app->helper($config->{dbix_helper}, $app->dbix); #add helper dbix
my $dbix_helper = $config->{dbix_helper} ||= 'dbix';
$app->attr($dbix_helper, sub {$dbix});
$app->helper($dbix_helper, $app->$dbix_helper); #add helper dbix
my $DSC = $config->{namespace} || 'DBIx::Simple::Class';
eval { Mojo::Loader->load($DSC) || $DSC->dbix($app->dbix) }
|| DBIx::Simple::Class->dbix($app->dbix); #do not forget

#make sure user classes have same dbix ready for use
eval { Mojo::Loader->load($DSC) || $DSC->dbix($app->$dbix_helper) }
|| DBIx::Simple::Class->dbix($app->$dbix_helper);
$DSC->DEBUG($config->{DEBUG});

$self->_load_classes($config);

Expand Down Expand Up @@ -189,7 +188,7 @@ L<Mojolicious::Plugin> and implements the following new ones.
Register plugin in L<Mojolicious> application.
=head1 config
=head2 config
This plugin own configuration. Returns a HASHref.
Expand Down
32 changes: 26 additions & 6 deletions t/advanced.t
Expand Up @@ -8,13 +8,28 @@ BEGIN {
use lib qw(t/lib);
}

use Test::More tests => 12;
use Test::More tests=>15;

package main;

use Mojolicious::Lite;
use Test::Mojo;
use Data::Dumper;

#Suppress some warnings from DBIx::Simple::Class during tests.
local $SIG{__WARN__} = sub {
if (
$_[0] =~ /(ddbix\sredefined
|SQL\sfrom)/x
)
{
my ($package, $filename, $line, $subroutine) = caller(1);
ok($_[0], $subroutine . " warns '$1' OK");
}
else {
warn $_[0];
}
};
plugin('Charset', {charset => 'UTF-8'});
my $config = {
database => ':memory:',
Expand All @@ -24,7 +39,7 @@ my $config = {
dbh_attributes => {sqlite_unicode => 1},
driver => 'SQLite',
onconnect_do => [],
dbix_helper => 'dbix',
dbix_helper => 'ddbix',
dsn => 'dbi:SQLite:database=:memory:'
};

Expand All @@ -43,10 +58,12 @@ TAB
$config->{load_classes} = ['My::User', 'Groups'];

isa_ok(plugin('DSC', $config), 'Mojolicious::Plugin::DSC');
ok(app->dbix->query($my_groups_table), 'app->dbix works');

ok(app->dbix->query('INSERT INTO my_groups ("group") VALUES(?)', 'pojo'),
'app->dbix->query works');
ok(app->ddbix->query($my_groups_table), 'app->ddbix works');

ok(app->ddbix->query('INSERT INTO my_groups ("group") VALUES(?)', 'pojo'),
'app->ddbix->query works');

my $group = My::Groups->find(1);
my $user = My::User->new(
group_id => $group->id,
Expand All @@ -63,10 +80,11 @@ my $your_config = {
dbix_helper => 'your_dbix',
dsn => 'dbi:SQLite:database=:memory:'
};

my $your_dbix = plugin('DSC', $your_config);

can_ok(app, 'your_dbix');
isnt(app->your_dbix, app->dbix, 'two schemas loaded');
isnt(app->your_dbix, app->ddbix, 'two schemas loaded');

get '/' => sub {
my $self = shift;
Expand All @@ -89,3 +107,5 @@ $t->content_is('Hello ' . $user->login_name . ' from group ' . $group->group . '

$t->post_form_ok('/edit/user', {id => 1, login_password => 'alabala123'})
->status_is(200)->content_is('New password for user петър is alabala123');

=comment
3 changes: 2 additions & 1 deletion t/lib/My.pm
@@ -1,6 +1,7 @@
{

package My; #our schema
package #hide
My; #our schema
use 5.010;
use strict;
use warnings;
Expand Down
3 changes: 2 additions & 1 deletion t/lib/My/Groups.pm
@@ -1,4 +1,5 @@
package My::Groups;
package #hide
My::Groups;
use strict;
use warnings;
use utf8;
Expand Down
3 changes: 2 additions & 1 deletion t/lib/My/User.pm
@@ -1,4 +1,5 @@
package My::User;
package #hide
My::User;
use Mojo::Base 'My';

sub TABLE {'users'}
Expand Down
8 changes: 5 additions & 3 deletions t/lib/Your.pm
@@ -1,17 +1,19 @@
{

package Your; #our second schema
package #hide
Your; #our second schema
use 5.010;
use strict;
use warnings;
use utf8;
use base qw(DBIx::Simple::Class);

sub dbix {

# Singleton DBIx::Simple instance
state $DBIx;
return ($DBIx = $_[1] ? $_[1] : $DBIx) || Carp::croak('DBIx::Simple is not instantiated for schema "Your"');
return ($DBIx = $_[1] ? $_[1] : $DBIx)
|| Carp::croak('DBIx::Simple is not instantiated for schema "Your"');
}
}
1;
3 changes: 2 additions & 1 deletion t/lib/Your/User.pm
@@ -1,4 +1,5 @@
package Your::User;
package #hide
Your::User;
use Mojo::Base 'Your';

sub TABLE {'users'}
Expand Down

0 comments on commit 644789a

Please sign in to comment.