Meerkat
view release on metacpan or search on metacpan
lib/Meerkat/Collection.pm view on Meta::CPAN
$self->_sync( $data => $obj );
$obj->_set_removed(0);
return 1;
}
else {
$obj->_set_removed(1);
return; # false means removed
}
}
sub update {
state $check = compile( Object, Object, HashRef );
my ( $self, $obj, $update ) = $check->(@_);
my $data = $self->_try_mongo_op(
update => sub {
$self->_mongo_collection->find_one_and_update( { _id => $obj->_id },
$update, { returnDocument => "after" } );
},
);
if ( ref $data ) {
lib/Meerkat/Role/Document.pm view on Meta::CPAN
#pod This command is intended for custom updates with unusual logic or operators.
#pod Many typical updates can be accomplished with the C<update_*> methods described
#pod below.
#pod
#pod For all update methods, you can use a MongoDB nested field label to modify
#pod values deep into a data structure. For example C<parents.father> refers to
#pod C<< $obj->parents->{father} >>.
#pod
#pod =cut
sub update {
state $check = compile( Object, HashRef );
my ( $self, $update ) = $check->(@_);
croak "The update method only accepts MongoDB update operators"
if grep { /^[^\$]/ } keys %$update;
return if $self->is_removed; # NOP
return $self->_collection->update( $self, $update );
}
#pod =method update_set
#pod
lib/Meerkat/Role/Document.pm view on Meta::CPAN
#pod
#pod Note this means that you can't set a defined value to undefined. To remove a
#pod field entirely, see L</update_clear>. If you need to make other structural
#pod changes, do it manually with the L</update> method.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod =cut
sub update_set {
state $check = compile( Object, Defined, Defined );
my ( $self, $field, $value ) = $check->(@_);
$self->__check_op( $field, any(qw/undef scalar object ARRAY HASH/) );
my $type = $self->__field_type( $self->_deep_field($field) );
my $target_type = $self->__field_type($value);
croak "Can't use update_set to change $type field '$field' to $target_type"
if $type eq none(qw/undef object/) && $type ne $target_type;
return $self->update( { '$set' => { "$field" => $value } } );
}
lib/Meerkat/Role/Document.pm view on Meta::CPAN
#pod
#pod Increments a field by a positive or negative value. This is the MongoDB
#pod C<$inc> operator. The field must be undefined or a numeric scalar value
#pod or an error will be thrown.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod =cut
sub update_inc {
state $check = compile( Object, Defined, Defined );
my ( $self, $field, $value ) = $check->(@_);
$self->__check_op( $field, any(qw/undef scalar/) );
my $current = $self->$field;
croak "Can't use update_inc on non-numeric field '$field'"
if defined $current && !looks_like_number($current);
return $self->update( { '$inc' => { "$field" => $value } } );
}
#pod =method update_push
lib/Meerkat/Role/Document.pm view on Meta::CPAN
#pod Pushes values onto an array reference field. This is the MongoDB C<$push>
#pod operator. The field must be undefined or an array reference or an error
#pod is thrown.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod
#pod =cut
sub update_push {
state $check = compile( Object, Defined, slurpy ArrayRef );
my ( $self, $field, $list ) = $check->(@_);
$self->__check_op( $field, any(qw/undef ARRAY/) );
return $self->update( { '$push' => { "$field" => { '$each' => $list } } } );
}
#pod =method update_add
#pod
#pod $obj->update_add( tags => qw/cool hot trendy/ );
#pod
#pod Pushes values onto an array reference field, but only if they do not already
#pod exist in the array. This is the MongoDB C<$addToSet> operator. The field
#pod must be undefined or an array reference or an error is thrown.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod =cut
sub update_add {
state $check = compile( Object, Defined, slurpy ArrayRef );
my ( $self, $field, $list ) = $check->(@_);
$self->__check_op( $field, any(qw/undef ARRAY/) );
return $self->update( { '$addToSet' => { "$field" => { '$each' => $list } } } );
}
#pod =method update_pop
#pod
#pod $obj->update_pop( 'tags' );
#pod
#pod Removes a value from the end of the array. This is the MongoDB C<$pop>
#pod operator with a direction of "1". The field must be undefined or an array
#pod reference or an error is thrown.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod =cut
sub update_pop {
state $check = compile( Object, Defined );
my ( $self, $field ) = $check->(@_);
$self->__check_op( $field, any(qw/undef ARRAY/) );
return $self->update( { '$pop' => { "$field" => 1 } } );
}
#pod =method update_shift
#pod
#pod $obj->update_shift( 'tags' );
#pod
#pod Removes a value from the front of the array. This is the MongoDB C<$pop>
#pod operator with a direction of "-1". The field must be undefined or an array
#pod reference or an error is thrown.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod
#pod =cut
sub update_shift {
state $check = compile( Object, Defined );
my ( $self, $field ) = $check->(@_);
$self->__check_op( $field, any(qw/undef ARRAY/) );
return $self->update( { '$pop' => { "$field" => -1 } } );
}
#pod =method update_remove
#pod
#pod $obj->update_remove( tags => qw/cool hot/ );
#pod
#pod Removes a list of values from the array. This is the MongoDB C<$pullAll>
#pod operator. The field must be undefined or an array reference or an error is
#pod thrown.
#pod
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod
#pod =cut
sub update_remove {
state $check = compile( Object, Defined, slurpy ArrayRef );
my ( $self, $field, $list ) = $check->(@_);
$self->__check_op( $field, any(qw/undef ARRAY/) );
return $self->update( { '$pullAll' => { "$field" => $list } } );
}
#pod =method update_clear
#pod
#pod $obj->update_clear( 'tags' );
#pod
#pod Removes a field from a document. This is the MongoDB C<$unset> operator.
#pod Returns true if the update is applied and synchronized. If the document has
#pod been removed, the method returns false and the object is marked as removed.
#pod
#pod Be sure not to clear any required fields.
#pod
#pod =cut
sub update_clear {
state $check = compile( Object, Defined );
my ( $self, $field ) = $check->(@_);
$self->__check_op( $field, any(qw/undef scalar object ARRAY HASH/) );
return $self->update( { '$unset' => { "$field" => undef } } );
}
#pod =method sync
#pod
#pod $obj->sync;
#pod
( run in 0.566 second using v1.01-cache-2.11-cpan-95122f20152 )