Skip to content

Commit

Permalink
Fix to update(\%args) with inflation from test case by Peter Rabbitson
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowcat-mst committed Mar 3, 2006
1 parent d0b55ba commit 9fcda14
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/DBIx/Class/InflateColumn.pm
Expand Up @@ -124,6 +124,18 @@ sub _inflated_column_op {
return $obj;
}

sub update {
my ($class, $attrs, @rest) = @_;
$attrs ||= {};
foreach my $key (keys %$attrs) {
if (ref $attrs->{$key}
&& exists $class->column_info($key)->{_inflate_info}) {
$attrs->{$key} = $class->_deflated_column($key, $attrs->{$key});
}
}
return $class->next::method($attrs, @rest);
}

sub new {
my ($class, $attrs, @rest) = @_;
$attrs ||= {};
Expand Down
1 change: 1 addition & 0 deletions t/lib/DBICTest/Schema.pm
Expand Up @@ -16,6 +16,7 @@ __PACKAGE__->load_classes(qw/
OneKey
#dummy
TwoKeys
Serialized
/]},
(
'FourKeys',
Expand Down
8 changes: 8 additions & 0 deletions t/lib/sqlite.sql
Expand Up @@ -131,4 +131,12 @@ CREATE TABLE self_ref (
name varchar NOT NULL
);

--
-- Table: serialized
--
CREATE TABLE serialized (
id INTEGER PRIMARY KEY NOT NULL,
serialized text NOT NULL
);

COMMIT;
30 changes: 29 additions & 1 deletion t/run/08inflate.tl
Expand Up @@ -4,7 +4,7 @@ my $schema = shift;
eval { require DateTime };
plan skip_all => "Need DateTime for inflation tests" if $@;

plan tests => 3;
plan tests => 5;

DBICTest::Schema::CD->inflate_column( 'year',
{ inflate => sub { DateTime->new( year => shift ) },
Expand All @@ -27,6 +27,34 @@ $cd->update;
($cd) = $schema->resultset("CD")->search( year => $now->year );
is( $cd->year->year, $now->year, 'deflate ok' );

use YAML;
DBICTest::Schema::Serialized->inflate_column( 'serialized',
{ inflate => sub { Load (shift) },
deflate => sub { die "Expecting a reference" unless (ref $_[0]); Dump (shift) } }
);
Class::C3->reinitialize;

my $complex1 = {
id => 1,
serialized => {
a => 1,
b => 2,
},
};

my $complex2 = {
id => 1,
serialized => [qw/a b 1 2/],
};

my $rs = $schema->resultset('Serialized');

my $entry = $rs->create($complex2);

ok($entry->update ($complex1), "update with hashref deflating ok");

ok($entry->update ($complex2), "update with arrayref deflating ok");

}

1;

0 comments on commit 9fcda14

Please sign in to comment.