Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 752831192109b179696bf7a7c91c534e822e4f50 0 parents
Красимир Беров authored
Showing with 201 additions and 0 deletions.
  1. +16 −0 Makefile.PL
  2. +16 −0 README.pod
  3. +143 −0 lib/Mojolicious/Plugin/DSC.pm
  4. +26 −0 t/basic.t
16 Makefile.PL
@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'Mojolicious::Plugin::DSC',
+ VERSION_FROM => 'lib/Mojolicious/Plugin/DSC.pm',
+ ABSTRACT_FROM => 'lib/Mojolicious/Plugin/DSC.pm',
+ AUTHOR => q{Красимир Беров <berov@cpan.org>},
+ PREREQ_PM => {'Mojolicious' => '3.14','DBIx::Simple::Class' =>'0.62'},
+ test => {TESTS => 't/*.t'},
+ dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
+ clean => { FILES => 'Mojolicious-Plugin-DSC-*' },
+
+);
16 README.pod
@@ -0,0 +1,16 @@
+=head1 NAME
+
+Mojolicious::Plugin::DSC - Mojolicious Plugin
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $self->plugin('DSC', $config);
+
+ # Mojolicious::Lite
+ plugin 'DSC', $config;
+
+=head1 DESCRIPTION
+
+L<Mojolicious::Plugin::DSC> is a L<Mojolicious> plugin.
+
143 lib/Mojolicious/Plugin/DSC.pm
@@ -0,0 +1,143 @@
+package Mojolicious::Plugin::DSC;
+use Mojo::Base 'Mojolicious::Plugin';
+use DBIx::Simple::Class;
+
+
+our $VERSION = '0.03';
+
+#some known good defaults
+my %COMMON_ATTRIBUTES = (
+ RaiseError => 1,
+ HandleError => sub { Carp::croak(shift) },
+ AutoCommit => 1,
+);
+
+my $DRIVER_ATTRIBUTES = {
+ 'mysql' => {mysql_enable_utf8 => 1, mysql_bind_type_guessing => 1},
+ 'SQLite' => {sqlite_unicode => 1},
+ 'Pg' => {pg_enable_utf8 => 1}
+};
+
+my $MEx = 'Mojo::Exception';
+
+sub register {
+ my ($c, $app, $config) = @_;
+
+ # This stuff is executed, when the plugin is loaded
+ # Config
+ $config ||= {};
+ $config->{load_classes} ||= [];
+ $config->{namespace} ||= '';
+ $config->{DEBUG} ||= ($app->mode =~ /^dev/ ? 1 : 0);
+ $config->{dbh_attributes} ||= {};
+
+ #prepared Data Source Name?
+ if (!$config->{dsn}) {
+ $config->{driver}
+ || $MEx->throw('Please choose and set a database driver like "mysql","SQLite","Pg"!..');
+ $config->{database} || $MEx->throw('Please set "database"!');
+ $config->{host} ||= 'localhost';
+ $config->{dsn} = 'dbi:'
+ . $config->{driver}
+ . ':database='
+ . $config->{database}
+ . ';host='
+ . $config->{host};
+ }
+ else {
+
+ #check if it is ok
+ my ($scheme, $attr_string, $attr_hash, $dsn);
+ ($scheme, $config->{driver}, $attr_string, $attr_hash, $dsn) =
+ DBI->parse_dsn($config->{dsn})
+ or $MEx->throw("Can't parse DBI DSN! dsn=>'$config->{dsn}'");
+ }
+
+ $MEx->throw('"load_classes" configuration directive '
+ . 'must contain a list of classes to load.')
+ unless (ref($config->{load_classes}) eq 'ARRAY');
+ if (@{$config->{load_classes}} && !$config->{namespace}) {
+ $MEx->throw('Please define namespace for your model classes!');
+ }
+ DBIx::Simple::Class->DEBUG($config->{DEBUG});
+
+ #ready... Go!
+ my $dbix = DBIx::Simple->connect(
+ $config->{dsn},
+ $config->{user},
+ $config->{password},
+ { %COMMON_ATTRIBUTES, %{$DRIVER_ATTRIBUTES->{$config->{driver}} || {}},
+ %{$config->{dbh_attributes}}
+ }
+ );
+ $config->{onconnect_do} ||= [];
+ if (!ref($config->{onconnect_do})) {
+ $config->{onconnect_do} = [$config->{onconnect_do}];
+ }
+ for my $sql ($config->{onconnect_do}) {
+ $dbix->dbh->do($sql) if $sql;
+ }
+ DBIx::Simple::Class->dbix($dbix);#do not forget
+ $config->{dbix_helper} ||= 'dbix';
+ $app->helper($config->{dbix_helper}, $dbix); #add helper dbix
+
+
+ if ($config->{namespace} && @{$config->{load_classes}}) {
+ my @classes = @{$config->{load_classes}};
+ my $namespace = $config->{namespace};
+ $namespace .= '::' unless $namespace =~ /\:\:$/;
+ foreach my $class (@classes) {
+ if ($class =~ /$namespace/) {
+ my $e = Mojo::Loader->load($class);
+ $MEx->throw($e) if $e;
+ next;
+ }
+ my $e = Mojo::Loader->load($namespace . $class);
+ $MEx->throw($e) if $e;
+ }
+ }
+ elsif ($config->{namespace} && !@{$config->{load_classes}}) {
+ my @classes = Mojo::Loader->search($config->{namespace});
+ foreach my $class (@classes) {
+ my $e = Mojo::Loader->load($class);
+ $MEx->throw($e) if $e;
+ }
+ }
+} #end register
+
+
+1;
+__END__
+
+=head1 NAME
+
+Mojolicious::Plugin::DSC - Mojolicious Plugin
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $self->plugin('DSC', $config);
+
+ # Mojolicious::Lite
+ plugin 'DSC', $config;
+
+=head1 DESCRIPTION
+
+L<Mojolicious::Plugin::DSC> is a L<Mojolicious> plugin.
+
+=head1 METHODS
+
+L<Mojolicious::Plugin::DSC> inherits all methods from
+L<Mojolicious::Plugin> and implements the following new ones.
+
+=head2 C<register>
+
+ $plugin->register(Mojolicious->new);
+
+Register plugin in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
+
+=cut
26 t/basic.t
@@ -0,0 +1,26 @@
+use Mojo::Base -strict;
+
+# Disable IPv6 and libev
+BEGIN {
+ $ENV{MOJO_NO_IPV6} = 1;
+ $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
+}
+
+use Test::More tests => 5;
+package main;
+use Mojolicious::Lite;
+use Test::Mojo;
+
+my $config = {};
+like((eval { plugin 'DSC' }, $@), qr/Please choose and set a database driver/, 'driver');
+$config->{driver} = 'SQLite';
+is((eval { plugin 'DSC',$config }, $@), 'Please set "database"!','database');
+
+get '/' => sub {
+ my $self = shift;
+ $self->render_text('Hello Mojo!');
+};
+
+my $t = Test::Mojo->new;
+$t->get_ok('/')->status_is(200)->content_is('Hello Mojo!');
+
Please sign in to comment.
Something went wrong with that request. Please try again.