Elive

 view release on metacpan or  search on metacpan

lib/Elive/DAO.pm  view on Meta::CPAN

    my @objs = (map {$class->construct( $_, connection => $connection )}
		@rows);
    #
    # possibly return a list of recurring meetings.
    #
    return wantarray? @objs : $objs[0];
}

=head2 live_entity

    my $user_ref
      = Elive::Entity->live_entity('http://test.org/User/1234567890');

Returns a reference to an object in the Elive::Entity cache. 

=cut

sub live_entity {
    my $class = shift;
    my $url = shift;

    return $Stored_Objects{ $url };
}

=head2 live_entities

    my $live_entities = Elive::Entity->live_entities;

    my $user_ref = $live_entities->{'http://test.org/User/1234567890'};

Returns a reference to the Elive::Entity cache. 

=cut

sub live_entities {
    my $class = shift;
    return \%Stored_Objects;
}

=head2 update

Abstract method to update entities. The following commits outstanding changes
to the object.

    $obj->{foo} = 'Foo';  # change foo attribute directly
    $foo->update;         # save

    $obj->bar('Bar');     # change bar via its accessor
    $obj->update;         # save

Updates may also be passed as parameters:

    # change and save foo and bar. All in one go.
    $obj->update({foo => 'Foo', bar => 'Bar'});

This method can be called from the class level. You will need to
supply the primary key and all mandatory fields. 

=cut

sub update {
    my ($self, $_update_data, %opt) = @_;

    die "attempted to update deleted record"
	if ($self->_deleted);

    my %params = %{ $opt{param} || {} };
    my %primary_key = map {$_ => 1} ($self->primary_key);
    my %updates;

        if (! ref $self) {
	# class level update
	$opt{connection} ||= $self->connection
	    if $self->connection;
	$self = $self->construct( $_update_data, %opt);
	for (keys %$self) {
	    $updates{$_} = $self->$_;
	}
    }
    elsif ($_update_data) {

	croak 'usage: $obj->update( \%data )'
	    unless (Elive::Util::_reftype($_update_data) eq 'HASH');

	my %update_data = %{ $_update_data };
	#
	# sift out things which are included in the data payload, but should
	# be parameters.
	#
	my %param_names = $self->params;
	foreach (grep {exists $update_data{$_}} %param_names) {
	    my $val = delete $update_data{$_};
	    $params{$_} = $val unless exists $params{$_};
	}

	$self->set( %update_data)
	    if (keys %update_data);
    }

    #
    # Write only changed properties.
    #
    my @updated_properties = ($opt{changed}
			      ? @{$opt{changed}} 
			      : $self->is_changed);

    #
    # merge in pending updates to the current entity.
    #

    foreach (@updated_properties, keys %primary_key) {

	my $update_val = $self->$_;

	if (exists $primary_key{$_} ) {
	    my $type = $self->property_types->{$_};
	    my $db_val = $self->_db_data->$_;
	    croak 'primary key field $_ updated - refusing to save'
		if $self->_cmp_col($type, $db_val, $update_val);
	}



( run in 1.032 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )