initial checkin
Brandon L. Black [Wed, 8 Feb 2006 02:22:22 +0000 (02:22 +0000)]
12 files changed:
Build.PL [new file with mode: 0644]
Changes [new file with mode: 0644]
MANIFEST [new file with mode: 0644]
MANIFEST.SKIP [new file with mode: 0644]
META.yml [new file with mode: 0644]
Makefile.PL [new file with mode: 0644]
README [new file with mode: 0644]
lib/Catalyst/Helper/Model/DBIC/Schema.pm [new file with mode: 0644]
lib/Catalyst/Model/DBIC/Schema.pm [new file with mode: 0644]
t/01use.t [new file with mode: 0644]
t/02pod.t [new file with mode: 0644]
t/03podcoverage.t [new file with mode: 0644]

diff --git a/Build.PL b/Build.PL
new file mode 100644 (file)
index 0000000..946e77f
--- /dev/null
+++ b/Build.PL
@@ -0,0 +1,21 @@
+use strict;
+use Module::Build;
+
+my %arguments = (
+    license            => 'perl',
+    module_name        => 'Catalyst::Model::DBIC::Schema',
+    requires           => {
+        'DBIx::Class'           => 0.05,
+        'Catalyst'              => 5.64,
+        'UNIVERSAL::require'    => 0,
+        'Class::Data::Accessor' => 0,
+        'Class::Accessor::Fast' => 0,
+    },
+    build_requires     => {
+        'Test::More'            => 0.32,
+    },
+    create_makefile_pl => 'passthrough',
+    create_readme      => 1,
+);
+
+Module::Build->new(%arguments)->create_build_script;
diff --git a/Changes b/Changes
new file mode 100644 (file)
index 0000000..7e9982d
--- /dev/null
+++ b/Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension Catalyst::Model::DBIC::Schema
+
+0.01  Not yet released
+        - First release
diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..0412395
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,11 @@
+Build.PL
+Changes
+lib/Catalyst/Helper/Model/DBIC/Schema.pm
+lib/Catalyst/Model/DBIC/Schema.pm
+MANIFEST                       This list of files
+t/01use.t
+t/02pod.t
+t/03podcoverage.t
+META.yml
+Makefile.PL
+README
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644 (file)
index 0000000..2ffcc10
--- /dev/null
@@ -0,0 +1,25 @@
+# Avoid version control files.
+\bRCS\b
+\bCVS\b
+,v$
+\B\.svn\b
+
+# Avoid Makemaker generated and utility files.
+\bMakefile$
+\bblib
+\bMakeMaker-\d
+\bpm_to_blib$
+\bblibdirs$
+^MANIFEST\.SKIP$
+
+# Avoid Module::Build generated and utility files.
+\bBuild$
+\b_build
+
+# Avoid temp and backup files.
+~$
+\.tmp$
+\.old$
+\.bak$
+\#$
+\b\.#
diff --git a/META.yml b/META.yml
new file mode 100644 (file)
index 0000000..e8e6422
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,22 @@
+---
+name: Catalyst-Model-DBIC-Schema
+version: 0.01
+author:
+  - 'Brandon L Black, C<blblack@gmail.com>'
+abstract: DBIx::Class::Schema Model Class
+license: perl
+requires:
+  Catalyst: 5.64
+  Class::Accessor::Fast: 0
+  Class::Data::Accessor: 0
+  DBIx::Class: 0.05
+  UNIVERSAL::require: 0
+build_requires:
+  Test::More: 0.32
+provides:
+  Catalyst::Helper::Model::DBIC::Schema:
+    file: lib/Catalyst/Helper/Model/DBIC/Schema.pm
+  Catalyst::Model::DBIC::Schema:
+    file: lib/Catalyst/Model/DBIC/Schema.pm
+    version: 0.01
+generated_by: Module::Build version 0.2611
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..51d31fd
--- /dev/null
@@ -0,0 +1,31 @@
+# Note: this file was auto-generated by Module::Build::Compat version 0.03
+    
+    unless (eval "use Module::Build::Compat 0.02; 1" ) {
+      print "This module requires Module::Build to install itself.\n";
+      
+      require ExtUtils::MakeMaker;
+      my $yn = ExtUtils::MakeMaker::prompt
+       ('  Install Module::Build now from CPAN?', 'y');
+      
+      unless ($yn =~ /^y/i) {
+       die " *** Cannot install without Module::Build.  Exiting ...\n";
+      }
+      
+      require Cwd;
+      require File::Spec;
+      require CPAN;
+      
+      # Save this 'cause CPAN will chdir all over the place.
+      my $cwd = Cwd::cwd();
+      my $makefile = File::Spec->rel2abs($0);
+      
+      CPAN::Shell->install('Module::Build::Compat')
+       or die " *** Cannot install without Module::Build.  Exiting ...\n";
+      
+      chdir $cwd or die "Cannot chdir() back to $cwd: $!";
+    }
+    eval "use Module::Build::Compat 0.02; 1" or die $@;
+    use lib '_build/lib';
+    Module::Build::Compat->run_build_pl(args => \@ARGV);
+    require Module::Build;
+    Module::Build::Compat->write_makefile(build_class => 'Module::Build');
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..f18d5b2
--- /dev/null
+++ b/README
@@ -0,0 +1,96 @@
+NAME
+    Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
+
+SYNOPSIS
+        package MyApp::Model::Foo;
+        use strict;
+        use base 'Catalyst::Model::DBIC::Schema';
+
+        __PACKAGE__->config(
+            schema_class => 'Foo::SchemaClass',
+            connect_info => [ 'dbi:Pg:dbname=foodb',
+                              'postgres',
+                              '',
+                              { AutoCommit => 1 },
+                            ],
+        );
+
+        1;
+
+        # In controller code:
+
+        # ->schema To access schema methods:
+        $c->model('Foo')->schema->source(...);
+
+        # Shortcut to the schema resultset monikers for ->search et al:
+        $c->model('Foo::Bar')->search(...);
+        # is the same as $c->model('Foo')->schema->resultset('Bar')->search(...);
+
+        # To get the composed schema for making new connections:
+        my $newconn = $c->model('Foo')->composed_schema->connect(...);
+
+        # Or the same thing via a convenience shortcut:
+        my $newconn = $c->model('Foo')->connect(...);
+
+        # or, if your schema works on different storage drivers:
+        my $newconn = $c->model('Foo')->composed_schema->clone();
+        $newconn->storage_type('::LDAP');
+        $newconn->connect(...);
+
+        # and again, a convenience shortcut
+        my $newconn = $c->model('Foo')->clone();
+        $newconn->storage_type('::LDAP');
+        $newconn->connect(...);
+
+DESCRIPTION
+    This is a Catalyst Model for DBIx::Class::Schema-based Models.
+
+CONFIG PARAMETERS
+    schema_class
+        This is the classname of your DBIx::Class::Schema Schema. It needs
+        to be findable in @INC, but it does not need to be underneath
+        "Catalyst::Model::".
+
+    connect_info
+        This is an arrayref of connection parameters, which are specific to
+        your "storage_type". For "::DBI", which is the only supported
+        "storage_type" in DBIx::Class at the time of this writing, the 4
+        parameters are your dsn, username, password, and connect options
+        hashref.
+
+    storage_type
+        Allows the use of a different "storage_type" than what is set in
+        your "schema_class" (which in turn defaults to "::DBI" if not set in
+        current DBIx::Class). Completely optional, and probably unneccesary
+        for most people, until other storage backends become available for
+        DBIx::Class.
+
+METHODS
+    new Instantiates the Model based on the above-documented ->config
+        parameters.
+
+    schema
+        Accessor which returns the connected schema being used by the this
+        model.
+
+    composed_schema
+        Accessor which returns the composed schema, which has no connection
+        info, which was used in constructing the "schema" above. Useful for
+        creating new connections based on the same schema/model.
+
+    clone
+        Shortcut for ->composed_schema->clone
+
+    connect
+        Shortcut for ->composed_schema->connect
+
+SEE ALSO
+    Catalyst, DBIx::Class, DBIx::Class::Schema, DBIx::Class::Schema::Loader
+
+AUTHOR
+    Brandon L Black, "blblack@gmail.com"
+
+COPYRIGHT
+    This program is free software, you can redistribute it and/or modify it
+    under the same terms as Perl itself.
+
diff --git a/lib/Catalyst/Helper/Model/DBIC/Schema.pm b/lib/Catalyst/Helper/Model/DBIC/Schema.pm
new file mode 100644 (file)
index 0000000..cc34ed9
--- /dev/null
@@ -0,0 +1,95 @@
+package Catalyst::Helper::Model::DBIC::Schema;
+
+use strict;
+
+=head1 NAME
+
+Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
+
+=head1 SYNOPSIS
+
+    script/create.pl model Foo DBIC::Schema Foo::SchemaClass dsn user password
+
+=head1 DESCRIPTION
+
+Helper for the DBIC Plain Models.
+
+=head2 METHODS
+
+=head3 mk_compclass
+
+=cut
+
+sub mk_compclass {
+    my ( $self, $helper, $schemaclass, $dsn, $user, $pass ) = @_;
+    $helper->{schemaclass} = $schemaclass || '';
+    $helper->{dsn}         = $dsn  || '';
+    $helper->{user}        = $user || '';
+    $helper->{pass}        = $pass || '';
+    my $file = $helper->{file};
+    $helper->render_file( 'compclass', $file );
+}
+
+=head1 SEE ALSO
+
+L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
+L<Catalyst::Response>, L<Catalyst::Helper>
+
+=head1 AUTHOR
+
+Brandon L Black, C<blblack@gmail.com>
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__DATA__
+
+__compclass__
+package [% class %];
+
+use strict;
+use base 'Catalyst::Model::DBIC::Schema';
+
+__PACKAGE__->config(
+    schema_class => '[% schemaclass %]
+    connect_info => [ '[% dsn %]',
+                      '[% user %]',
+                      '[% pass %]',
+                      {
+                          RaiseError         => 1,
+                          PrintError         => 0,
+                          ShowErrorStatement => 1,
+                          TraceLevel         => 0,
+                          AutoCommit         => 1,
+                      }
+                    ],
+);
+
+=head1 NAME
+
+[% class %] - Catalyst DBIC Plain Model
+
+=head1 SYNOPSIS
+
+See L<[% app %]>
+
+=head1 DESCRIPTION
+
+Catalyst::Model::DBIC::Schema Model
+
+=head1 AUTHOR
+
+[% author %]
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
diff --git a/lib/Catalyst/Model/DBIC/Schema.pm b/lib/Catalyst/Model/DBIC/Schema.pm
new file mode 100644 (file)
index 0000000..543ab88
--- /dev/null
@@ -0,0 +1,176 @@
+package Catalyst::Model::DBIC::Schema;
+
+use strict;
+use base qw/Catalyst::Base Class::Accessor::Fast Class::Data::Accessor/;
+use NEXT;
+use UNIVERSAL::require;
+use Carp;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->mk_classdata('composed_schema');
+__PACKAGE__->mk_accessors('schema');
+
+=head1 NAME
+
+Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
+
+=head1 SYNOPSIS
+
+    package MyApp::Model::Foo;
+    use strict;
+    use base 'Catalyst::Model::DBIC::Schema';
+
+    __PACKAGE__->config(
+        schema_class => 'Foo::SchemaClass',
+        connect_info => [ 'dbi:Pg:dbname=foodb',
+                          'postgres',
+                          '',
+                          { AutoCommit => 1 },
+                        ],
+    );
+
+    1;
+
+    # In controller code:
+
+    # ->schema To access schema methods:
+    $c->model('Foo')->schema->source(...);
+
+    # Shortcut to the schema resultset monikers for ->search et al:
+    $c->model('Foo::Bar')->search(...);
+    # is the same as $c->model('Foo')->schema->resultset('Bar')->search(...);
+
+    # To get the composed schema for making new connections:
+    my $newconn = $c->model('Foo')->composed_schema->connect(...);
+
+    # Or the same thing via a convenience shortcut:
+    my $newconn = $c->model('Foo')->connect(...);
+
+    # or, if your schema works on different storage drivers:
+    my $newconn = $c->model('Foo')->composed_schema->clone();
+    $newconn->storage_type('::LDAP');
+    $newconn->connect(...);
+
+    # and again, a convenience shortcut
+    my $newconn = $c->model('Foo')->clone();
+    $newconn->storage_type('::LDAP');
+    $newconn->connect(...);
+
+=head1 DESCRIPTION
+
+This is a Catalyst Model for L<DBIx::Class::Schema>-based Models.
+
+=head1 CONFIG PARAMETERS
+
+=over 4
+
+=item schema_class
+
+This is the classname of your L<DBIx::Class::Schema> Schema.  It needs
+to be findable in C<@INC>, but it does not need to be underneath
+C<Catalyst::Model::>.
+
+=item connect_info
+
+This is an arrayref of connection parameters, which are specific to your
+C<storage_type>.  For C<::DBI>, which is the only supported C<storage_type>
+in L<DBIx::Class> at the time of this writing, the 4 parameters are your
+dsn, username, password, and connect options hashref.
+
+=item storage_type
+
+Allows the use of a different C<storage_type> than what is set in your
+C<schema_class> (which in turn defaults to C<::DBI> if not set in current
+L<DBIx::Class>).  Completely optional, and probably unneccesary for most
+people, until other storage backends become available for L<DBIx::Class>.
+
+=back
+
+=head1 METHODS
+
+=over 4
+
+=item new
+
+Instantiates the Model based on the above-documented ->config parameters.
+
+=item schema
+
+Accessor which returns the connected schema being used by the this model.
+
+=item composed_schema
+
+Accessor which returns the composed schema, which has no connection info,
+which was used in constructing the C<schema> above.  Useful for creating
+new connections based on the same schema/model.
+
+=item clone
+
+Shortcut for ->composed_schema->clone
+
+=item connect
+
+Shortcut for ->composed_schema->connect
+
+=back
+
+=cut
+
+sub new {
+    my ( $self, $c ) = @_;
+    $self = $self->NEXT::new($c);
+    
+    my $class = ref($self);
+    my $model_name = $class;
+    $model_name =~ s/^[\w:]+::(?:Model|M):://;
+
+    foreach (qw/ connect_info schema_class /) {
+        croak "->config->{$_} must be defined for this model"
+            unless $self->{$_};
+    }
+
+    my $schema_class = $self->{schema_class};
+
+    $schema_class->require
+        or croak "Cannot load schema class '$schema_class': $@";
+
+    $self->composed_schema($schema_class->compose_namespace($class));
+    $self->schema($self->composed_schema->clone);
+    $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
+    $self->schema->connect(@{$self->{connect_info}});
+
+    no strict 'refs';
+    foreach my $moniker ($self->schema->sources) {
+        *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub {
+            shift;
+            shift->model($model_name)->schema->resultset($moniker);
+        }
+    }
+
+    return $self;
+}
+
+# convenience method
+sub clone { shift->composed_schema->clone(@_); }
+
+# convenience method
+sub connect { shift->composed_schema->connect(@_); }
+
+=head1 SEE ALSO
+
+L<Catalyst>, L<DBIx::Class>, L<DBIx::Class::Schema>,
+L<DBIx::Class::Schema::Loader>
+
+=head1 AUTHOR
+
+Brandon L Black, C<blblack@gmail.com>
+
+=head1 COPYRIGHT
+
+This program is free software, you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
+1;
diff --git a/t/01use.t b/t/01use.t
new file mode 100644 (file)
index 0000000..d566133
--- /dev/null
+++ b/t/01use.t
@@ -0,0 +1,4 @@
+use strict;
+use Test::More tests => 1;
+
+BEGIN { use_ok('Catalyst::Model::DBIC::Schema') }
diff --git a/t/02pod.t b/t/02pod.t
new file mode 100644 (file)
index 0000000..2c5bf3a
--- /dev/null
+++ b/t/02pod.t
@@ -0,0 +1,8 @@
+use Test::More;
+
+eval "use Test::Pod 1.14";
+plan skip_all => 'Test::Pod 1.14 required' if $@;
+plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
+
+all_pod_files_ok();
+
diff --git a/t/03podcoverage.t b/t/03podcoverage.t
new file mode 100644 (file)
index 0000000..d91be5e
--- /dev/null
@@ -0,0 +1,7 @@
+use Test::More;
+
+eval "use Test::Pod::Coverage 1.04";
+plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
+plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
+
+all_pod_coverage_ok();