From: luke Date: Mon, 25 May 2009 23:56:14 +0000 (+0000) Subject: custom rs methods working X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=46845551adc234ce4437faf1e94fb477f56863ce;p=dbsrgits%2FDBIx-Class-ResultSet-WithMetaData.git custom rs methods working --- diff --git a/lib/DBIx/Class/ResultSet/WithMetaData.pm b/lib/DBIx/Class/ResultSet/WithMetaData.pm index ede534d..b3004db 100644 --- a/lib/DBIx/Class/ResultSet/WithMetaData.pm +++ b/lib/DBIx/Class/ResultSet/WithMetaData.pm @@ -57,7 +57,10 @@ method display () { return ($self->was_row) ? $rows[0] : \@rows; } -method add_row_info (Int :$id, HashRef :$info) { +method add_row_info (Int :$id, :$row, HashRef :$info) { + if ($row) { + $id = $self->_mk_id(row => { $row->get_columns }); + } unless ($self->find($id)) { warn $id; die 'invalid id passed to add_row_info'; diff --git a/t/custom_methods.t b/t/custom_methods.t new file mode 100644 index 0000000..1ece36a --- /dev/null +++ b/t/custom_methods.t @@ -0,0 +1,52 @@ +#!perl + +use Test::More tests => 2; +use lib qw(t/lib); +use DBICTest; +use Data::Dumper; + +# set up and populate schema +ok(my $schema = DBICTest->init_schema(), 'got schema'); + +{ + my $artist_rs = $schema->resultset('Artist')->order_by(col => 'artistid')->display(); + warn Dumper($artist_rs); + is_deeply($artist_rs, [ + { + 'artistid' => '1', + 'name' => 'Caterwauler McCrae' + }, + { + 'artistid' => '2', + 'name' => 'Random Boy Band' + }, + { + 'artistid' => '3', + 'name' => 'We Are Goth' + } + ], 'ordered display returned as expected'); +} + +{ + my $artists = $schema->resultset('Artist')->order_by(col => 'artistid')->with_substr->display(); + is_deeply($artists, [ + { + 'artistid' => '1', + 'name' => 'Caterwauler McCrae', + 'substr' => 'Cat' + }, + { + 'artistid' => '2', + 'name' => 'Random Boy Band', + 'substr' => 'Ran' + }, + { + 'artistid' => '3', + 'name' => 'We Are Goth', + 'substr' => 'We ' + } + ], 'display with substring okay'); +} + + + diff --git a/t/lib/DBICTest/Schema/ResultSet/Artist.pm b/t/lib/DBICTest/Schema/ResultSet/Artist.pm index e78b331..0b96f93 100644 --- a/t/lib/DBICTest/Schema/ResultSet/Artist.pm +++ b/t/lib/DBICTest/Schema/ResultSet/Artist.pm @@ -1,6 +1,15 @@ package DBICTest::Schema::ResultSet::Artist; use Moose; +use MooseX::Method::Signatures; extends 'DBICTest::Schema::ResultSet'; +method with_substr () { + foreach my $row ($self->all) { + my $substr = substr($row->name, 0, 3); + $self->add_row_info(row => $row, info => { substr => $substr }); + } + return $self; +} + 1;