added 1.000
Johannes Plunien [Sat, 14 Jun 2008 14:03:05 +0000 (14:03 +0000)]
14 files changed:
Changes [new file with mode: 0644]
MANIFEST [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/DBIx/Class/ResultSet/HashRef.pm [new file with mode: 0644]
t/00-use.t [new file with mode: 0644]
t/10-hashref.t [new file with mode: 0644]
t/98-pod_coverage.t [new file with mode: 0644]
t/99-pod.t [new file with mode: 0644]
t/lib/TestSchema.pm [new file with mode: 0644]
t/lib/TestSchema/Role.pm [new file with mode: 0644]
t/lib/TestSchema/User.pm [new file with mode: 0644]
t/lib/TestSchema/UserRole.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
new file mode 100644 (file)
index 0000000..ed12c46
--- /dev/null
+++ b/Changes
@@ -0,0 +1,5 @@
+Revision history for DBIx-Class-ResultSet-HashRef
+
+0.01  Sat Jun 14 2008
+       Initial release.
+
diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..4bdd4a0
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,25 @@
+Changes
+inc/Module/AutoInstall.pm
+inc/Module/Install/AutoInstall.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Include.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
+inc/Module/Install.pm
+lib/DBIx/Class/ResultSet/HashRef.pm
+Makefile.PL
+MANIFEST
+META.yml
+README
+t/00-use.t
+t/10-hashref.t
+t/98-pod_coverage.t
+t/99-pod.t
+t/lib/TestSchema.pm
+t/lib/TestSchema/Role.pm
+t/lib/TestSchema/User.pm
+t/lib/TestSchema/UserRole.pm
\ No newline at end of file
diff --git a/META.yml b/META.yml
new file mode 100644 (file)
index 0000000..f6d20fa
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,22 @@
+--- 
+abstract: Adds syntatic sugar to skip the fancy objects
+author: 
+  - Johannes Plunien <plu@cpan.org>
+build_requires: 
+  File::Temp: 0
+  Test::More: 0
+distribution_type: module
+generated_by: Module::Install version 0.68
+license: perl
+meta-spec: 
+  url: http://module-build.sourceforge.net/META-spec-v1.3.html
+  version: 1.3
+name: DBIx-Class-ResultSet-HashRef
+no_index: 
+  directory: 
+    - inc
+    - t
+requires: 
+  DBIx::Class: 0
+  perl: 5.6.1
+version: 1.000
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..3576607
--- /dev/null
@@ -0,0 +1,18 @@
+use inc::Module::Install 0.67;
+
+if ( -e 'MANIFEST.SKIP' ) {
+    system( 'pod2text lib/DBIx/Class/ResultSet/HashRef.pm > README' );
+}
+
+perl_version '5.006001';
+
+name 'DBIx-Class-ResultSet-HashRef';
+all_from 'lib/DBIx/Class/ResultSet/HashRef.pm';
+
+requires 'DBIx::Class';
+
+test_requires 'Test::More';
+test_requires 'File::Temp';
+
+auto_install;
+WriteAll;
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..637f696
--- /dev/null
+++ b/README
@@ -0,0 +1,67 @@
+NAME
+    DBIx::Class::ResultSet::HashRef - Adds syntatic sugar to skip the fancy
+    objects
+
+SYNOPSIS
+        # in your resultsource class
+        __PACKAGE__->resultset_class( 'DBIx::Class::ResultSet::HashRef' );
+    
+        # in your calling code
+        my $rs = $schema->resultset('User')->search( { } )->hashref_rs;
+        while (my $row = $rs->next) {
+            print Dumper $row;
+        }
+    
+        You can chain up every L<DBIx::Class::ResultSet> method to ->hashref_rs:
+    
+        * ->hashref_rs->all (same as ->hashref_array)
+
+        * ->hashref_rs->first (same as ->hashref_first)
+    
+DESCRIPTION
+    This is a simple way to allow you to set result_class to
+    DBIx::Class::ResultClass::HashRefInflator to skip the fancy objects.
+
+INSTALLATION
+        perl Makefile.PL
+        make
+        make test
+        make install
+
+METHODS
+  hashref_rs( )
+    Sets result_class to DBIx::Class::ResultClass::HashRefInflator and
+    returns the resultset.
+
+  hashref_array( )
+    Calls ->hashref_rs->all and returns depending on the calling context an
+    array or an reference to an array.
+
+        my $rs = $schema->resultset('User')->search( { } )->hashref_array;
+        print Dumper $rs;
+
+        my @rs = $schema->resultset('User')->search( { } )->hashref_array;
+        print Dumper @rs;
+
+  hashref_first( )
+    Returns the first row of the resultset inflated by
+    DBIx::Class::ResultClass::HashRefInflator.
+
+        my $first_row = $schema->resultset('User')->search( { } )->hashref_first;
+        print Dumper $first_row
+
+AUTHOR
+    Johannes Plunien <plu@cpan.org>
+
+COPYRIGHT AND LICENSE
+    Copyright 2008 by Johannes Plunien
+
+    This library is free software; you can redistribute it and/or modify it
+    under the same terms as Perl itself.
+
+    Thanks to mst for his patience.
+
+SEE ALSO
+    * DBIx::Class
+    * DBIx::Class::ResultClass::HashRefInflator
+
diff --git a/lib/DBIx/Class/ResultSet/HashRef.pm b/lib/DBIx/Class/ResultSet/HashRef.pm
new file mode 100644 (file)
index 0000000..6f87cf8
--- /dev/null
@@ -0,0 +1,111 @@
+package DBIx::Class::ResultSet::HashRef;
+
+use warnings;
+use strict;
+use base qw( DBIx::Class::ResultSet );
+use DBIx::Class::ResultClass::HashRefInflator;
+
+our $VERSION = '1.000';
+
+=head1 NAME
+
+DBIx::Class::ResultSet::HashRef - Adds syntatic sugar to skip the fancy objects
+
+=head1 SYNOPSIS
+
+    # in your resultsource class
+    __PACKAGE__->resultset_class( 'DBIx::Class::ResultSet::HashRef' );
+    
+    # in your calling code
+    my $rs = $schema->resultset('User')->search( { } )->hashref_rs;
+    while (my $row = $rs->next) {
+        print Dumper $row;
+    }
+    
+    You can chain up every L<DBIx::Class::ResultSet> method to ->hashref_rs:
+    
+    * ->hashref_rs->all (same as ->hashref_array)
+
+    * ->hashref_rs->first (same as ->hashref_first)
+    
+=head1 DESCRIPTION
+
+This is a simple way to allow you to set result_class to L<DBIx::Class::ResultClass::HashRefInflator> to
+skip the fancy objects.
+
+=head1 INSTALLATION
+
+    perl Makefile.PL
+    make
+    make test
+    make install
+
+=head1 METHODS
+
+=head2 hashref_rs( )
+
+Sets result_class to L<DBIx::Class::ResultClass::HashRefInflator> and returns the resultset.
+
+=cut
+
+sub hashref_rs {
+    my ($self) = @_;
+    $self->result_class('DBIx::Class::ResultClass::HashRefInflator');
+    return $self;
+}
+
+=head2 hashref_array( )
+
+Calls ->hashref_rs->all and returns depending on the calling context an array or an reference to an array. 
+
+    my $rs = $schema->resultset('User')->search( { } )->hashref_array;
+    print Dumper $rs;
+
+    my @rs = $schema->resultset('User')->search( { } )->hashref_array;
+    print Dumper @rs;
+
+=cut
+
+sub hashref_array {
+    return wantarray ? shift->hashref_rs->all : [ shift->hashref_rs->all ];
+}
+
+=head2 hashref_first( )
+
+Returns the first row of the resultset inflated by L<DBIx::Class::ResultClass::HashRefInflator>.
+
+    my $first_row = $schema->resultset('User')->search( { } )->hashref_first;
+    print Dumper $first_row
+
+=cut
+
+sub hashref_first {
+    return shift->hashref_rs->first;
+}
+
+=head1 AUTHOR
+
+Johannes Plunien E<lt>plu@cpan.orgE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008 by Johannes Plunien
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+Thanks to mst for his patience.
+
+=head1 SEE ALSO
+
+=over 4 
+
+=item * L<DBIx::Class>
+
+=item * L<DBIx::Class::ResultClass::HashRefInflator>
+
+=back
+
+=cut
+
+1;
diff --git a/t/00-use.t b/t/00-use.t
new file mode 100644 (file)
index 0000000..20368bb
--- /dev/null
@@ -0,0 +1,7 @@
+use Test::More tests => 1;
+
+BEGIN {
+use_ok( 'DBIx::Class::ResultSet::HashRef' );
+}
+
+diag( "Testing DBIx::Class::ResultSet::HashRef $DBIx::Class::ResultSet::HashRef::VERSION" );
diff --git a/t/10-hashref.t b/t/10-hashref.t
new file mode 100644 (file)
index 0000000..73e0e5d
--- /dev/null
@@ -0,0 +1,316 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+    eval "use DBD::SQLite ();";
+    plan skip_all => 'DBD::SQLite required to run this test' if $@;
+
+    eval "use SQL::Translator ();";
+    plan skip_all => 'SQL::Translator required to run this test' if $@;
+
+    plan( tests => 10 );
+}
+
+use lib 't/lib';
+use TestSchema;
+use File::Temp;
+
+# setup
+my ( undef, $db ) = File::Temp::tempfile();
+my $schema = TestSchema->connect( "dbi:SQLite:dbname=${db}", undef, undef );
+$schema->deploy;
+
+my @users = qw/root toor daemon operator bin tty/;
+my @roles = qw/admin superuser user/;
+
+@users = $schema->populate( 'User' => [ ['login'] => ( map { [$_] } @users ) ] );
+@roles = $schema->populate( 'Role' => [ ['name']  => ( map { [$_] } @roles ) ] );
+
+my $u          = 1;
+my @user_roles = ();
+foreach my $user (@users) {
+    my $r = 0;
+    foreach my $role (@roles) {
+        next if $r >= $u;
+        push @user_roles, [ $user->id, $role->id ];
+        $r++;
+    }
+    $u++;
+    $u = 1 if $u > scalar @roles;
+}
+
+@user_roles = $schema->populate( 'UserRole' => [ [qw/user_id role_id/] => @user_roles ] );
+
+{
+    my $rs = $schema->resultset('User')->search(
+        {},
+        {
+            prefetch => { user_role => [qw/role/] },
+            order_by => 'me.id ASC'
+        }
+    )->hashref_array;
+
+    is_deeply(
+        $rs,
+        [
+            {
+                'id'        => '1',
+                'login'     => 'root',
+                'user_role' => [
+                    {
+                        'role' => {
+                            'id'   => '1',
+                            'name' => 'admin'
+                        },
+                        'role_id' => '1',
+                        'user_id' => '1'
+                    }
+                ]
+            },
+            {
+                'id'        => '2',
+                'login'     => 'toor',
+                'user_role' => [
+                    {
+                        'role' => {
+                            'id'   => '1',
+                            'name' => 'admin'
+                        },
+                        'role_id' => '1',
+                        'user_id' => '2'
+                    },
+                    {
+                        'role' => {
+                            'id'   => '2',
+                            'name' => 'superuser'
+                        },
+                        'role_id' => '2',
+                        'user_id' => '2'
+                    }
+                ]
+            },
+            {
+                'id'        => '3',
+                'login'     => 'daemon',
+                'user_role' => [
+                    {
+                        'role' => {
+                            'id'   => '1',
+                            'name' => 'admin'
+                        },
+                        'role_id' => '1',
+                        'user_id' => '3'
+                    },
+                    {
+                        'role' => {
+                            'id'   => '2',
+                            'name' => 'superuser'
+                        },
+                        'role_id' => '2',
+                        'user_id' => '3'
+                    },
+                    {
+                        'role' => {
+                            'id'   => '3',
+                            'name' => 'user'
+                        },
+                        'role_id' => '3',
+                        'user_id' => '3'
+                    }
+                ]
+            },
+            {
+                'id'        => '4',
+                'login'     => 'operator',
+                'user_role' => [
+                    {
+                        'role' => {
+                            'id'   => '1',
+                            'name' => 'admin'
+                        },
+                        'role_id' => '1',
+                        'user_id' => '4'
+                    }
+                ]
+            },
+            {
+                'id'        => '5',
+                'login'     => 'bin',
+                'user_role' => [
+                    {
+                        'role' => {
+                            'id'   => '1',
+                            'name' => 'admin'
+                        },
+                        'role_id' => '1',
+                        'user_id' => '5'
+                    },
+                    {
+                        'role' => {
+                            'id'   => '2',
+                            'name' => 'superuser'
+                        },
+                        'role_id' => '2',
+                        'user_id' => '5'
+                    }
+                ]
+            },
+            {
+                'id'        => '6',
+                'login'     => 'tty',
+                'user_role' => [
+                    {
+                        'role' => {
+                            'id'   => '1',
+                            'name' => 'admin'
+                        },
+                        'role_id' => '1',
+                        'user_id' => '6'
+                    },
+                    {
+                        'role' => {
+                            'id'   => '2',
+                            'name' => 'superuser'
+                        },
+                        'role_id' => '2',
+                        'user_id' => '6'
+                    },
+                    {
+                        'role' => {
+                            'id'   => '3',
+                            'name' => 'user'
+                        },
+                        'role_id' => '3',
+                        'user_id' => '6'
+                    }
+                ]
+            }
+        ],
+        'hashref_array'
+    );
+}
+
+{
+    my @rs = $schema->resultset('User')->search( {}, { order_by => 'me.id ASC' } )->hashref_array;
+    is_deeply(
+        \@rs,
+        [
+            {
+                'id'    => '1',
+                'login' => 'root'
+            },
+            {
+                'id'    => '2',
+                'login' => 'toor'
+            },
+            {
+                'id'    => '3',
+                'login' => 'daemon'
+            },
+            {
+                'id'    => '4',
+                'login' => 'operator'
+            },
+            {
+                'id'    => '5',
+                'login' => 'bin'
+            },
+            {
+                'id'    => '6',
+                'login' => 'tty'
+            }
+        ]
+    );
+}
+
+{
+    my $rs = $schema->resultset('User')->search(
+        {},
+        {
+            prefetch => { user_role => [qw/role/] },
+            order_by => 'me.id DESC'
+        }
+    )->hashref_rs->next;
+    is_deeply(
+        $rs,
+        {
+            'id'        => '6',
+            'login'     => 'tty',
+            'user_role' => [
+                {
+                    'role' => {
+                        'id'   => '1',
+                        'name' => 'admin'
+                    },
+                    'role_id' => '1',
+                    'user_id' => '6'
+                },
+                {
+                    'role' => {
+                        'id'   => '2',
+                        'name' => 'superuser'
+                    },
+                    'role_id' => '2',
+                    'user_id' => '6'
+                },
+                {
+                    'role' => {
+                        'id'   => '3',
+                        'name' => 'user'
+                    },
+                    'role_id' => '3',
+                    'user_id' => '6'
+                }
+            ]
+        },
+        'hashref_rs->next'
+    );
+}
+
+{
+    my $expected_users = [
+        {
+            'id'    => '1',
+            'login' => 'root'
+        },
+        {
+            'id'    => '2',
+            'login' => 'toor'
+        },
+        {
+            'id'    => '3',
+            'login' => 'daemon'
+        },
+        {
+            'id'    => '4',
+            'login' => 'operator'
+        },
+        {
+            'id'    => '5',
+            'login' => 'bin'
+        },
+        {
+            'id'    => '6',
+            'login' => 'tty'
+        }
+    ];
+    my $rs = $schema->resultset('User')->search( {}, { order_by => 'me.id ASC' } )->hashref_rs;
+    while ( my $row = $rs->next ) {
+        my $user = shift(@$expected_users);
+        is_deeply( $row, $user, "hashref_rs in while loop, user: " . $user->{login} );
+    }
+}
+
+{
+    my $first_row = $schema->resultset('User')->search( { login => 'root' } )->hashref_first;
+    is_deeply(
+        $first_row,
+        {
+            'id'    => '1',
+            'login' => 'root'
+        },
+        "hashref_first"
+    );
+}
diff --git a/t/98-pod_coverage.t b/t/98-pod_coverage.t
new file mode 100644 (file)
index 0000000..703f91d
--- /dev/null
@@ -0,0 +1,6 @@
+#!perl -T
+
+use Test::More;
+eval "use Test::Pod::Coverage 1.04";
+plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@;
+all_pod_coverage_ok();
diff --git a/t/99-pod.t b/t/99-pod.t
new file mode 100644 (file)
index 0000000..976d7cd
--- /dev/null
@@ -0,0 +1,6 @@
+#!perl -T
+
+use Test::More;
+eval "use Test::Pod 1.14";
+plan skip_all => "Test::Pod 1.14 required for testing POD" if $@;
+all_pod_files_ok();
diff --git a/t/lib/TestSchema.pm b/t/lib/TestSchema.pm
new file mode 100644 (file)
index 0000000..9f975a6
--- /dev/null
@@ -0,0 +1,9 @@
+package TestSchema;
+
+use strict;
+use warnings;
+use base qw( DBIx::Class::Schema );
+
+__PACKAGE__->load_classes;
+
+1;
diff --git a/t/lib/TestSchema/Role.pm b/t/lib/TestSchema/Role.pm
new file mode 100644 (file)
index 0000000..c20aea3
--- /dev/null
@@ -0,0 +1,27 @@
+package TestSchema::Role;
+
+use strict;
+use warnings;
+use base qw( DBIx::Class );
+
+__PACKAGE__->load_components(qw( Core ));
+__PACKAGE__->table('role');
+__PACKAGE__->add_columns(
+    id => {
+        data_type         => 'int',
+        is_auto_increment => 1,
+        is_nullable       => 0,
+        default_value     => undef,
+        size              => 10,
+    },
+    name => {
+        data_type   => 'varchar',
+        size        => 16,
+        is_nullable => 0,
+    },
+);
+__PACKAGE__->set_primary_key("id");
+__PACKAGE__->has_many( user_role => 'TestSchema::UserRole', 'role_id' );
+__PACKAGE__->many_to_many( users => 'user_role', 'user' );
+
+1;
diff --git a/t/lib/TestSchema/User.pm b/t/lib/TestSchema/User.pm
new file mode 100644 (file)
index 0000000..d18228e
--- /dev/null
@@ -0,0 +1,28 @@
+package TestSchema::User;
+
+use strict;
+use warnings;
+use base qw( DBIx::Class );
+
+__PACKAGE__->load_components(qw( Core ));
+__PACKAGE__->table('user');
+__PACKAGE__->add_columns(
+    id => {
+        data_type         => 'int',
+        is_auto_increment => 1,
+        is_nullable       => 0,
+        default_value     => undef,
+        size              => 10,
+    },
+    login => {
+        data_type   => 'varchar',
+        size        => 16,
+        is_nullable => 0,
+    },
+);
+__PACKAGE__->set_primary_key("id");
+__PACKAGE__->has_many( user_role => 'TestSchema::UserRole', 'user_id' );
+__PACKAGE__->many_to_many( roles => 'user_role', 'role' );
+__PACKAGE__->resultset_class('DBIx::Class::ResultSet::HashRef');
+
+1;
diff --git a/t/lib/TestSchema/UserRole.pm b/t/lib/TestSchema/UserRole.pm
new file mode 100644 (file)
index 0000000..5868268
--- /dev/null
@@ -0,0 +1,25 @@
+package TestSchema::UserRole;
+
+use strict;
+use warnings;
+use base qw( DBIx::Class );
+
+__PACKAGE__->load_components("Core");
+__PACKAGE__->table("user_role");
+__PACKAGE__->add_columns(
+    user_id => {
+        data_type     => "INT",
+        default_value => undef,
+        is_nullable   => 0,
+        size          => 10
+    },
+    role_id => {
+        data_type     => "INT",
+        default_value => undef,
+        is_nullable   => 0,
+        size          => 10
+    },
+);
+__PACKAGE__->set_primary_key(qw/user_id role_id/);
+__PACKAGE__->belongs_to( user => 'TestSchema::User', { id => "user_id" } );
+__PACKAGE__->belongs_to( role => 'TestSchema::Role', { id => "role_id" } );