Skip to content

Commit

Permalink
Merge 57dd62b into 0be61e7
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbruner committed Dec 1, 2020
2 parents 0be61e7 + 57dd62b commit 01b6a52
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/Meerkat/Collection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ the MongoDB L<count|MongoDB::Collection/count> method.
sub count {
state $check = compile( Object, Optional [HashRef] );
my ( $self, $query ) = $check->(@_);
return $self->_try_mongo_op( count => sub { $self->_mongo_collection->count($query) }
$query = {} unless $query;
return $self->_try_mongo_op( count => sub { $self->_mongo_collection->count_documents($query) }
);
}

Expand All @@ -139,15 +140,14 @@ if one occurs. This is a shorthand for the same query via C<find_one>:
$person->find_one( { _id => $id } );
However, C<find_id> can take either a scalar C<_id> or a L<MongoDB::OID> object
However, C<find_id> can take either a scalar C<_id> or a L<BSON::OID> object
as an argument.
=cut

sub find_id {
state $check = compile( Object, Defined );
my ( $self, $id ) = $check->(@_);
$id = ref($id) eq 'MongoDB::OID' ? $id : MongoDB::OID->new($id);
my $data =
$self->_try_mongo_op(
find_id => sub { $self->_mongo_collection->find_one( { _id => $id } ) } );
Expand Down
8 changes: 4 additions & 4 deletions lib/Meerkat/Role/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use MooseX::Storage::Engine;
use Carp qw/croak/;
use Scalar::Util qw/blessed/;
use Syntax::Keyword::Junction qw/any none/;
use MongoDB::OID;
use BSON::OID;
use Type::Params qw/compile/;
use Types::Standard qw/slurpy :types/;
use Scalar::Util qw/looks_like_number/; # XXX crude but fast
Expand All @@ -27,7 +27,7 @@ with Storage;
# pass through OID's without modification as MongoDB will
# consume/provide them; pass through Meerkat::Collection
# as Meerkat will strip/add as necessary
for my $type (qw/MongoDB::OID Meerkat::Collection DateTime DateTime::Tiny/) {
for my $type (qw/BSON::OID Meerkat::Collection DateTime DateTime::Tiny/) {
MooseX::Storage::Engine->add_custom_type_handler(
$type => (
expand => sub { shift },
Expand All @@ -53,8 +53,8 @@ has _collection => (

has _id => (
is => 'ro',
isa => 'MongoDB::OID',
default => sub { MongoDB::OID->new },
isa => 'BSON::OID',
default => sub { BSON::OID->new },
);

has _removed => (
Expand Down
5 changes: 3 additions & 2 deletions lib/Meerkat/Types.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ coerce MeerkatDateTime, (
from Num, via { MDT->new( epoch => $_ ) },
from InstanceOf ['DateTime'], via { MDT->new( epoch => $_->epoch ) },
from InstanceOf ['DateTime::Tiny'], via { MDT->new( epoch => $_->DateTime->epoch ) },
from InstanceOf ['BSON::Time'], via { MDT->new( epoch => $_->epoch ) },
#>>>
);

Expand All @@ -31,7 +32,7 @@ coerce MeerkatDateTime, (
MooseX::Storage::Engine->add_custom_type_handler(
MeerkatDateTime,
(
expand => sub { MDT->new( epoch => $_[0] ) },
expand => sub { MDT->new( epoch => "$_[0]" ) },
collapse => sub { DateTime->from_epoch( epoch => $_[0]->epoch ) },
)
);
Expand Down Expand Up @@ -59,7 +60,7 @@ This module defines Moose types and coercions.
=head2 MeerkatDateTime
This type is a L<Meerkat::DateTime>. It defines coercions from C<Num> (an epoch value),
L<DateTime> and L<DateTime::Tiny>.
L<DateTime>, L<DateTime::Tiny>, and L<BSON::Time>.
It also sets up a L<MooseX::Storage> type handler that 'collapses' to a
DateTime object for storage by the MongoDB client, but 'expands' from an epoch
Expand Down
5 changes: 1 addition & 4 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ test 'round trip' => sub {
my $obj2 = $self->person->find_id( $obj1->_id );
is_deeply( $obj2, $obj1, "retrieve first object from database by OID" );

my $obj3 = $self->person->find_id( $obj1->_id->value );
is_deeply( $obj3, $obj1, "retrieve first object from database by string" );

ok( my $cursor = $self->person->find( { name => $obj1->name } ), "find query ran" );
isa_ok( $cursor, 'Meerkat::Cursor' );
my $obj4 = $cursor->next;
Expand All @@ -54,7 +51,7 @@ test 'not found' => sub {
my $self = shift;
ok( my $obj1 = $self->create_person, "created object" );

my $fake_id = MongoDB::OID->new;
my $fake_id = BSON::OID->new;
is( $self->person->find_id($fake_id),
undef, "find_id on non-existent doc returns undef" );
is( $self->person->find_one( { _id => $fake_id } ),
Expand Down
2 changes: 1 addition & 1 deletion t/datetime.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# COPYRIGHTuse strict;
use strict;
use warnings;
use Test::Roo;
use Test::Deep '!blessed';
Expand Down

0 comments on commit 01b6a52

Please sign in to comment.