Backed_Objects

 view release on metacpan or  search on metacpan

lib/Backed_Objects.pm  view on Meta::CPAN

=head2 outputter

This method should return a value used to output a view of the DB
(for example it may be used to output HTML files and be a hash whose values
are HTML templates).

Example:

  use File::Slurp;

  sub outputter {
    my ($self) = @_;
    my $template_dir = "$ENV{DOCUMENT_ROOT}/templates";
    return { main_tmpl => read_file("$template_dir/main.html"),
             announce_tmpl => read_file("$template_dir/announce.html") };
  }

The default implementation returns C<undef>.

=cut

sub outputter { }

=head2 insert, update, delete

  HTML_DB->insert($obj);
  HTML_DB->update($obj);
  HTML_DB->delete($id);

These functions update the view based on the value C<$obj> from the DB.
They are to be called when an object is inserted, updated, or deleted in
the DB.

If you've overridden the C<do_insert>, C<do_update>, or C<do_delete> methods,
then C<insert>, C<update>, or C<delete> methods update the database before
updating the view.

Note that C<insert> methods calls both C<on_update> and C<on_insert> methods
(as well as some other methods, see the source).

C<insert> and C<update> also call C<post_process>.

C<update> also calls C<before_update> before calling C<do_update>.
The C<before_update> method can be used to update the data based on
old data in the DB, before the DB is updated by C<do_update>.

=cut

# Calls both on_update() and on_insert()
sub insert {
  my ($self, $obj) = @_;
  die "Inserting an object into DB second time!" if $self->id($obj);
  $self->do_insert($obj);
  $self->post_process($obj);
  $self->on_insert($obj);
  $self->on_update($obj);
  $self->on_order_change;
  $self->on_any_change;
}

sub update {
  my ($self, $obj) = @_;
  die "Updating an object not in DB!" unless $self->id($obj);
  $self->before_update($obj);
  $self->do_update($obj);
  $self->post_process($obj);
  $self->on_update($obj);
  $self->on_any_change;
}

sub delete {
  my ($self, $id) = @_;
  $self->do_delete($id);
  $self->on_order_change;
  $self->on_delete($id);
  $self->on_any_change;
}

=head2 on_update, on_update_one

C<on_update> method it called when an object in the database is updated or after
a new object is inserted.

C<on_update_one> is the method called by C<on_update>. The C<on_update_one>
method is meant to update view of one object. Contrary to this, C<on_update>
may be overridden to update several objects by calling C<on_update_one> several
times. For example, when updating title of a HTML file, we may want to update
two more HTML files with titles of prev/next links dependent on the title of
this object.

By default C<on_update_one> calls the C<output> method to update the view of the
object.

=cut

sub on_update {
  my ($self, $obj) = @_;
  $self->on_update_one($obj);
}

sub on_update_one {
  my ($self, $obj) = @_;
  $self->output(scalar($self->outputter), $obj, 1);
}

=head2 on_insert, on_delete, on_order_change, before_update

  sub on_insert {
    my ($self, $obj) = @_;
    ...
  }

  sub on_delete {
    my ($self, $id) = @_;
    ...
  }

  sub on_order_change {
    my ($self) = @_;
    ...
  }



( run in 3.390 seconds using v1.01-cache-2.11-cpan-9169edd2b0e )