[Bast-commits] r6799 - in DBIx-Class-Schema-PopulateMore/truck: . lib/DBIx/Class/Schema lib/DBIx/Class/Schema/PopulateMore/Inflator t

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Fri Jun 26 18:48:23 GMT 2009


Author: jnapiorkowski
Date: 2009-06-26 18:48:22 +0000 (Fri, 26 Jun 2009)
New Revision: 6799

Added:
   DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore/Inflator/Find.pm
Modified:
   DBIx-Class-Schema-PopulateMore/truck/Changes
   DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore.pm
   DBIx-Class-Schema-PopulateMore/truck/t/01-schema.t
Log:
added new inflater for finding rows via unique ids

Modified: DBIx-Class-Schema-PopulateMore/truck/Changes
===================================================================
--- DBIx-Class-Schema-PopulateMore/truck/Changes	2009-06-26 15:39:54 UTC (rev 6798)
+++ DBIx-Class-Schema-PopulateMore/truck/Changes	2009-06-26 18:48:22 UTC (rev 6799)
@@ -1,5 +1,8 @@
 Revision history for Perl extension DBIx-Class-Schema-PopulateMore.
 
+0.09 Friday, June 26, 2009
+    - Added a Find inflator, with docs and a test case
+
 0.08 Friday, June 26, 2009
     - Fixed some documentation issues
     - fixed the copyright

Added: DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore/Inflator/Find.pm
===================================================================
--- DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore/Inflator/Find.pm	                        (rev 0)
+++ DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore/Inflator/Find.pm	2009-06-26 18:48:22 UTC (rev 6799)
@@ -0,0 +1,67 @@
+package DBIx::Class::Schema::PopulateMore::Inflator::Find;
+
+use Moose;
+extends 'DBIx::Class::Schema::PopulateMore::Inflator';
+
+=head1 NAME
+
+DBIx::Class::Schema::PopulateMore::Inflator::Find - Inflate via ResultSet->find
+ 
+=head1 SYNOPSIS
+
+	!Find:Rating.10 => $schema->resultset('Rating')->find(10);
+	!Find:Rating.[key=10] => $schema->resultset('Rating')->find(10);
+
+=head1 DESCRIPTION
+
+Given a Source.$value, do a $schema->Resultset('Source')->find($value) and use
+that value.  We can't find anything, throw an exception.
+
+=head1 ATTRIBUTES
+
+This class defines the following attributes.
+
+=head1 METHODS
+
+This module defines the following methods.
+
+=head2 inflate($command, $string)
+
+This is called by Populate's dispatcher, when there is a match.
+
+=cut
+
+sub inflate
+{ 
+	my ($self, $command, $string) = @_;
+	my ($source, $id) = split('\.', $string);
+
+	if(my $resultset = $command->schema->resultset($source)) {
+		if($id =~m/^\[.+\]$/) {
+			my ($key, $value) = ($id =~m/^\[\s*(\w+?)=(\w+?)\s*\]$/);
+			$id = {$key, $value};
+		} else { warn '.........\n\n'; }
+		if(my $result = $resultset->find($id)) {
+			return $result;
+		} else {
+			confess "Can't find result for '$id' in '$source'";
+		}
+	} else {
+		confess "Can't find resultset for $source in $string";
+	}
+	return;
+}
+
+
+=head1 AUTHOR
+
+Please see L<DBIx::Class::Schema::PopulateMore> For authorship information
+
+=head1 LICENSE
+
+Please see L<DBIx::Class::Schema::PopulateMore> For licensing terms.
+
+=cut
+
+
+1;

Modified: DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore.pm
===================================================================
--- DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore.pm	2009-06-26 15:39:54 UTC (rev 6798)
+++ DBIx-Class-Schema-PopulateMore/truck/lib/DBIx/Class/Schema/PopulateMore.pm	2009-06-26 18:48:22 UTC (rev 6799)
@@ -11,11 +11,11 @@
 
 =head1 VERSION
 
-Version 0.08
+Version 0.09
 
 =cut
 
-our $VERSION = '0.08';
+our $VERSION = '0.09';
 
 =head1 SYNOPSIS
 
@@ -108,6 +108,13 @@
 L<DBIx::Class::InflateColumn::DateTime> and mark your column data type as 
 'datetime' or similar.
 
+=item Find
+
+Used for when you want the value of something that you expect already exists
+in the database (but for which you didn't just populatemore for, use 'Index'
+for that case.) Use cases for this include lookup style tables, like 'Status'
+or 'Gender', 'State', etc. which you may already have installed.
+
 It's trivial to write more; please feel free to post me your contributions.
 
 =back
@@ -120,7 +127,7 @@
 
 This module defines the following methods.
 
-=head2 populate_more ($ArrayRef)
+=head2 populate_more ($ArrayRef||@Array)
 
 Given an arrayref formatted as in the L</SYNOPSIS> example, populate a rows in
 a database.  Confesses on errors.  

Modified: DBIx-Class-Schema-PopulateMore/truck/t/01-schema.t
===================================================================
--- DBIx-Class-Schema-PopulateMore/truck/t/01-schema.t	2009-06-26 15:39:54 UTC (rev 6798)
+++ DBIx-Class-Schema-PopulateMore/truck/t/01-schema.t	2009-06-26 18:48:22 UTC (rev 6799)
@@ -1,7 +1,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 31;
+use Test::More tests => 35;
 use DBIx::Class::Schema::PopulateMore::Test::Schema;
 
 ok my $schema = DBIx::Class::Schema::PopulateMore::Test::Schema->connect_and_setup
@@ -183,3 +183,24 @@
 	=> 'got correct number of friends for mike';
 	
 }
+
+## Extra tests
+
+ok my $extra = [
+	{Person	=> {
+		fields => ['name', 'age', 'gender'],
+		data => {
+			joe => ['joe', 19, '!Find:Gender.[label=male]'],
+		}}},
+], 'Created extra';
+
+ok my %index2 = $schema->populate_more($extra)
+  => 'Successful populated again.';
+
+ok my $joe = $schema->resultset('Person')->search({name=>'joe'})->first,
+  => 'Got a Person';
+
+is $joe->age, 19, 'Joe is 19';
+
+
+




More information about the Bast-commits mailing list