MongoDBx-Class

 view release on metacpan or  search on metacpan

lib/MongoDBx/Class/Document.pm  view on Meta::CPAN

saved to the database. For example:

	my $doc = find_one($id);

	$doc->set_author('Sir Arthur Conan Doyle');
	$doc->set_year(1895);
	$doc->update;

Will effectively result in something like this being performed:

	$coll->update({ _id => $doc->_id }, $collapsed_doc, $options)

You can pass an options hash-ref just like with the C<update()> method
of L<MongoDBx::Class::Collection>, but only if you pass an update object
also.

Returns the output of L<MongoDB::Collection>'s original C<update()> method.

=cut

sub update {
	my $self = shift;

	if (scalar @_ && ref $_[0] eq 'HASH') {
		$_[0]->{_class} = $self->_class;
		my $doc = $self->_connection->collapse($_[0]);
		delete $doc->{_class};

		# update the database entry
		my $ret = $self->_collection->update({ _id => $self->_id }, { '$set' => $doc }, $_[1]);

		# update the object itself
		$self->_update_self;

		return $ret;
	} else {
		my $doc = { _class => $self->_class };
		foreach ($self->meta->get_all_attributes) {
			my $name = $_->name;
			next if $name eq '_collection' || $name eq '_class';
			my $val = $self->$name;
			next unless defined $val;

			$name =~ s/^_// if ($_->{isa} eq 'MongoDBx::Class::CoercedReference' ||
					    $_->{isa} eq 'ArrayOfMongoDBx::Class::CoercedReference' ||
					    ($_->documentation && $_->documentation eq 'MongoDBx::Class::EmbeddedDocument')
					   );

			$doc->{$name} = $val;
		}
		return $self->_collection->update({ _id => $self->_id }, $self->_connection->collapse($doc), $_[1]);
	}
}

=head2 delete()

=head2 remove()

Both methods are equivalent. They are shortcut methods for invoking the
collection's C<remove()> method on this document only. So, umm, they remove
the document. But note that this operation does not cascade, so documents
which are considered dependant on this document (such as those that reference
it with C<belongs_to>) will not be removed too.

=cut

sub delete {
	my $self = shift;

	$self->_collection->remove({ _id => $self->_id });
}

sub remove { shift->delete }

=head1 INTERNAL METHODS

The following methods are only to be used internally.

=head2 _database()

Convenience method, shortcut for C<< $doc->_collection->_database >>.

=cut

sub _database {
	shift->_collection->_database;
}

=head2 _connection()

Convenience method, shortcut for C<< $doc->_database->_connection >>.

=cut

sub _connection {
	shift->_database->_connection;
}

=head2 _attributes()

Returns a list of names of all attributes the document object has, minus
'_collection' and '_class', sorted alphabetically.

=cut

sub _attributes {
	my @names;
	foreach (shift->meta->get_all_attributes) {
		next if $_->name =~ m/^_(class|collection)$/;
		if ($_->{isa} =~ m/MongoDBx::Class::CoercedReference/ || ($_->documentation && $_->documentation eq 'MongoDBx::Class::EmbeddedDocument')) {
			my $name = $_->name;
			$name =~ s/^_//;
			push(@names, $name);
		} else {
			push(@names, $_->name);
		}
	}

	return sort @names;
}



( run in 0.508 second using v1.01-cache-2.11-cpan-39bf76dae61 )