cvsuser     04/09/02 14:01:03

  Modified:    App-Repository/lib/App RepositoryObject.pm
  Log:
  finally implemented this and it works
  
  Revision  Changes    Path
  1.2       +69 -31    p5ee/App-Repository/lib/App/RepositoryObject.pm
  
  Index: RepositoryObject.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Repository/lib/App/RepositoryObject.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -w -r1.1 -r1.2
  --- RepositoryObject.pm       12 Oct 2002 03:03:48 -0000      1.1
  +++ RepositoryObject.pm       2 Sep 2004 21:01:03 -0000       1.2
  @@ -1,13 +1,12 @@
   
   #############################################################################
  -## $Id: RepositoryObject.pm,v 1.1 2002/10/12 03:03:48 spadkins Exp $
  +## $Id: RepositoryObject.pm,v 1.2 2004/09/02 21:01:03 spadkins Exp $
   #############################################################################
   
   package App::RepositoryObject;
   
   use App;
  -use App::Service;
  [EMAIL PROTECTED] = ( "App::Service" );
  +use App::Repository;
   
   use strict;
   
  @@ -19,15 +18,7 @@
   
       use App::RepositoryObject;
   
  -    $context = App->context();
  -
  -    $rep = $context->service("Repository");  # or ...
  -    $rep = $context->repository();
  -
  -    $obj = $rep->object($table, $key);
  -
  -    $value  = $rep->get($attrib);
  -    $rep->set($attrib, $value);
  +    ...
   
   =cut
   
  @@ -36,15 +27,20 @@
   A RepositoryObject is an object whose state is stored in a repository.
   It is a base class for many business classes.
   
  -All RepositoryObjects are created using the $rep->object() method,
  +All RepositoryObjects are created using the $rep->get_object() or
  +$rep->get_objects() methods,
   and they all have the following attributes.
   
  -    $self->{repository} - the Repository which the object came from
  -    $self->{context}    - the Context the object is running in
  -    $self->{table}      - the table name associated with the object
  -    $self->{key}        - the unique identifier of the object in the
  +    $self->{_repository} - the Repository which the object came from
  +    $self->{_table}      - the table name associated with the object
  +    $self->{_key}        - the unique identifier of the object in the
                             table in the repository
   
  +I am considering adding the following standard attribute, but I have
  +not yet decided whether I should.
  +
  +    $self->{_context}    - the Context the object is running in
  +
   =cut
   
   #############################################################################
  @@ -62,8 +58,9 @@
   =head2 get()
   
       * Signature: $value = $obj->get($attrib);
  -    * Param:     $attrib      string
  -    * Return:    $value       any
  +    * Signature: $value = $obj->get($attrib, $options);
  +    * Param:     $attrib      string,ARRAY
  +    * Return:    $value       any,ARRAY
       * Throws:    App::Exception
       * Since:     0.01
   
  @@ -71,14 +68,41 @@
   
       $value = $obj->get($attrib);
   
  -Gets the value of an attribute of an object.
  +Gets the value of one or more attributes of an object.
   
   =cut
   
   sub get {
  -    my ($self, $attrib) = @_;
  -    return undef if (! defined $self->{key});
  -    return $self->{repository}->get_value($self->{table}, $self->{key}, $attrib);
  +    my ($self, $attrib, $options) = @_;
  +    my (@values, $value);
  +    if (ref($attrib) eq "ARRAY") {
  +        foreach my $column (@$attrib) {
  +            if (exists $self->{$attrib}) {
  +                push(@values, $self->{$attrib});
  +            }
  +            else {
  +                @values = ();
  +                last;
  +            }
  +        }
  +        if ($#values == -1) {
  +            @values = $self->{_repository}->get($self->{_table}, $self->{_key}, 
$attrib);
  +            for (my $i = 0; $i <= $#$attrib; $i++) {
  +                $self->{$attrib->[$i]} = $values[$i];
  +            }
  +        }
  +        return(@values);
  +    }
  +    else {
  +        if (exists $self->{$attrib}) {
  +            $value = $self->{$attrib};
  +        }
  +        else {
  +            $value = $self->{_repository}->get($self->{_table}, $self->{_key}, 
$attrib);
  +            $self->{$attrib} = $value;
  +        }
  +        return($value);
  +    }
   }
   
   #############################################################################
  @@ -88,9 +112,10 @@
   =head2 set()
   
       * Signature: $obj->set($attrib, $value);
  -    * Param:     $attrib      string
  -    * Param:     $value       any
  -    * Return:    void
  +    * Signature: $obj->set($attrib, $value, $options);
  +    * Param:     $attrib      string,ARRAY
  +    * Param:     $value       any,ARRAY
  +    * Param:     $options     any,ARRAY
       * Throws:    App::Exception
       * Since:     0.01
   
  @@ -98,14 +123,27 @@
   
       $obj->set($attrib, $value);
   
  -Sets the value of an attribute of an object.
  +Sets the value of one or more attributes of an object.
   
   =cut
   
   sub set {
  -    my ($self, $attrib, $value) = @_;
  -    $self->{repository}->set_value($self->{table}, $self->{key}, $attrib, $value)
  -        if ($self->{key});
  +    my ($self, $attrib, $value, $options) = @_;
  +    my $nrows = $self->{_repository}->set($self->{_table}, $self->{_key}, $attrib, 
$value);
  +    if (ref($attrib) eq "ARRAY") {
  +        for (my $i = 0; $i <= $#$attrib; $i++) {
  +            if ($nrows && exists $self->{$attrib->[$i]}) {
  +                $self->{$attrib->[$i]} = $value->[$i];
  +            }
  +        }
  +    }
  +    else {
  +        if ($nrows && exists $self->{$attrib}) {
  +            $self->{$attrib} = $value;
  +        }
  +    }
  +    die "can't set($attrib, $value) on object[$self->{_table}.$self->{_key}]" if 
(!$nrows);
  +    return($nrows);
   }
   
   =head1 ACKNOWLEDGEMENTS
  
  
  

Reply via email to