From: David Kamholz Date: Sat, 15 Oct 2005 06:26:34 +0000 (+0000) Subject: add get_columns and set_columns methods to Row.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=076a6864aae318ebaf7ccbe00e7e9edc2b20d0fa;p=dbsrgits%2FDBIx-Class-Historic.git add get_columns and set_columns methods to Row.pm --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 4308e9c..38f6a82 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -176,6 +176,19 @@ sub get_column { return undef; } +=item get_columns + + my %data = $obj->get_columns; + +Fetch all column values at once. + +=cut + +sub get_columns { + my $self = shift; + return map { $_ => $self->get_column($_) } $self->columns; +} + =item set_column $obj->set_column($col => $val); @@ -194,6 +207,29 @@ sub set_column { return $ret; } +=item set_columns + + my $copy = $orig->copy({ $col => $val, ... }); + +Set more than one column value at once. + +=cut + +sub set_columns { + my ($self,$data) = @_; + while (my ($col,$val) = each %$data) { + $self->set_column($col,$val); + } +} + +=item copy + + my $copy = $orig->copy({ change => $to, ... }); + +Insert a new row with the specified changes. + +=cut + =item store_column $obj->store_column($col => $val); @@ -220,12 +256,6 @@ sub _row_to_object { return $new; } -=item copy - - my $copy = $orig->copy({ change => $to, ... }); - -=cut - sub copy { my ($self, $changes) = @_; my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self); diff --git a/t/run/01core.tl b/t/run/01core.tl index 13b4d34..d97f31f 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -1,6 +1,6 @@ sub run_tests { -plan tests => 23; +plan tests => 27; my @art = DBICTest::Artist->search({ }, { order_by => 'name DESC'}); @@ -74,6 +74,22 @@ is($new_again->ID, 'DBICTest::Artist|artistid=4', 'unique object id generated co is(DBICTest::Artist->count, 4, 'count ok'); +my $cd = DBICTest::CD->find(1); +my %cols = $cd->get_columns; + +cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok'); + +is($cols{title}, 'Spoonful of bees', 'get_columns values ok'); + +%cols = ( title => 'Forkful of bees', year => 2005); +$cd->set_columns(\%cols); + +is($cd->title, 'Forkful of bees', 'set_columns ok'); + +is($cd->year, 2005, 'set_columns ok'); + +$cd->discard_changes; + # insert_or_update $new = DBICTest::Track->new( { trackid => 100,