Skip to content

Commit

Permalink
Keep dates and bools untouched when collapsing hashrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
diegok committed Mar 23, 2017
1 parent fad8fc3 commit fa72d82
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{{$NEXT}}
[ BUGS ]
* Keep bools and dates untouched when collapsing hashrefs

1.01 2017-03-12 22:36:57+01:00 Europe/Madrid
[ BUGS ]
* Fixed MongoDB min version on dist.ini file

Expand Down
15 changes: 6 additions & 9 deletions lib/Mongoose/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,13 @@ sub _collapse {
id => $ref_id
);
}
elsif ( ref($value) =~ /^(?: DateTime(?:\:\:Tiny)? | boolean )$/x ) { # Types accepted by the driver
return $value;
}
else {
return $self->_unbless( $value, $class, @scope );
}

return $self->_unbless( $value, $class, @scope );
}
elsif ( ref $value eq 'ARRAY' ) {
my ( @arr, $aryclass );
my @arr;
for my $item ( @$value ) {
$aryclass ||= blessed( $item );
my $aryclass ||= blessed( $item );
if ( $aryclass && $aryclass->does('Mongoose::EmbeddedDocument') ) {
push @arr, $item->collapse(@scope, $self);
}
Expand Down Expand Up @@ -136,7 +132,8 @@ sub _unbless {
$ret = \@objs;
}
}
else { # non-moose class
# non-moose class
elsif ( $class !~ /^(?: DateTime(?:\:\:Tiny)? | boolean )$/x ) { # Types accepted by the driver
my $reftype = reftype($obj);
if ( $reftype eq 'ARRAY' ) { $ret = [@$obj] }
elsif ( $reftype eq 'SCALAR' ) { $ret = $$obj }
Expand Down
28 changes: 19 additions & 9 deletions t/types.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use Test::More;
use boolean qw/true false/;
use DateTime;

{
package Person;
Expand Down Expand Up @@ -34,8 +35,9 @@ use boolean qw/true false/;
use Mongoose::Class;
use boolean qw/true false/;
with 'Mongoose::Document';
has name => ( is => 'ro', isa => 'Str' );
has ready => ( is => 'rw', isa => 'Bool', default => sub {false} );
has name => ( is => 'ro', isa => 'Str' );
has ready => ( is => 'rw', isa => 'Bool', default => sub {false} );
has updated => ( is => 'rw', isa => 'HashRef', default => sub {{}} );
}

{
Expand Down Expand Up @@ -69,14 +71,22 @@ use boolean qw/true false/;
is ref($t2->hh->{aa}), 'ARRAY', 'expanded hash into array';

subtest 'Booleans roundtip' => sub {
ok( my $obj = OtherThing->new( name => 'Ambar' ), 'Create new object with state false (default)' );
isa_ok( $obj->ready, 'boolean', 'State is a boolean' );
ok( $obj->save, 'Save it' );
ok( $obj = OtherThing->find_one($obj->_id), 'Get it back from store' );
isa_ok( $obj->ready, 'boolean', 'State is still a boolean' );
ok( my $obj = OtherThing->new( name => 'Ambar' ), 'Create new object with state false (default)' );
isa_ok( $obj->ready, 'boolean', 'State is a boolean' );
ok( $obj->save, 'Save it' );
ok( $obj = OtherThing->find_one($obj->_id), 'Get it back from store' );
isa_ok( $obj->ready, 'boolean', 'State is still a boolean' );

is( OtherThing->count({ ready => false }), 1, 'Count objects matching a boolean' );
is( OtherThing->count({ ready => 0 }), 0, 'Count objects matching a pseudo-boolean' );
is( OtherThing->count({ ready => false }), 1, 'Count objects matching a boolean' );
is( OtherThing->count({ ready => 0 }), 0, 'Count objects matching a pseudo-boolean' );
};

subtest 'DateTime on hashrefs roundtip' => sub {
ok( my $obj = OtherThing->find_one, 'Get one object' );
ok( $obj->updated({ x => DateTime->now }), 'Set a HashRef[DateTime] attribute' );
ok( $obj->save, 'Save it' );
ok( $obj = OtherThing->find_one($obj->_id), 'Get it back from store' );
isa_ok( $obj->updated->{x}, 'DateTime', 'Attribute is ok' );
};
}

Expand Down

0 comments on commit fa72d82

Please sign in to comment.