Class-AlzaboWrapper

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

- The tests foolishly assumed you had MySQL available. Now the tests
try to use MySQL or Postgres, whichever is available. Reported by Joe
McMahon.


0.12  2005-10-22

- Having a function Class::AlzaboWrapper::Cursor() and a class named
Class::AlzaboWrapper::Cursor apparently confuses the heck out of the
Perl interpreter.  Renamed Cursor() to NewCursor().  The former
method name, cursor(), works as well.


0.11  2005-10-21

- Fix a number of methods which were accidentally broken in 0.10.


0.10  2005-10-21

* This release deprecates a number of methods, though it should be

Changes  view on Meta::CPAN

- Added a next_as_hash method to Class::AlzaboWrapper::Cursor.  Patch
by Eric Waters.

- Allow create() to take extra parameters just like new() does, so
that when _init() is eventually called it can work the same way for
both create() and new().


0.06  2004-09-23

- The cursor object new method now accepts a constructor_method
argument.


0.05  2004-06-25

- Added a new method, alzabo_attributes(), which can be called to find
out what methods were created by calling import.  Also added
_record_attribute_creation() so a custom subclass can record that it
created an attribute method.

README  view on Meta::CPAN

    *   Table()

        This method returns the Alzabo table object associated with the
        subclass. This may also be called as an object method.

    *   AlzaboAttributes()

        Returns a list of accessor methods that were created based on the
        columns in the class's associated table.

    *   NewCursor ($cursor)

        Given an "Alzabo::Runtime::Cursor" object (either a row or join
        cursor), this method returns a new "Class::AlzaboWrapper::Cursor"
        object.

   Object methods
    *   row_object()

        This method returns the "Alzabo::Runtime::Row" object associated
        with the given subclass object. So, for our hypothetical
        "WebTalk::User" class, this would return an object representing the
        underlying row from the User table.

README  view on Meta::CPAN

   Class::AlzaboWrapper methods
    The "Class::AlzaboWrapper" module has a method it provides:

    *   TableToClass($table)

        Given an Alzabo table object, this method returns its associated
        subclass.

   Cursors
    When using this module, you need to use the
    "Class::AlzaboWrapper::Cursor" module to wrap Alzabo's cursor objects,
    so that objects the cursor returns are of the appropriate subclass, not
    plain "Alzabo::Runtime::Row" objects. The "Cursor()" method provides
    some syntactic sugar for creating "Class::AlzaboWrapper::Cursor"
    objects.

   Attributes created by subclasses
    If you want to record the accessor methods your subclass makes so they
    are available via "AlzaboAttributes()", you can call the
    "_RecordAttributeCreation()" method, which expects two arguments. The
    first argument is the class for which the method was created and the
    second is the name of the method.

lib/Class/AlzaboWrapper.pm  view on Meta::CPAN


sub Columns { shift->table->columns(@_) }
*Column = \&Columns;
# deprecated
*columns = \&Columns;
*column = \&Columns;

sub NewCursor
{
    my $self = shift;
    my $cursor = shift;

    return
        Class::AlzaboWrapper::Cursor->new
            ( cursor => $cursor );
}
# deprecated
*cursor = \&NewCursor;


sub TableToClass { $TableToClass{ $_[1]->name } }
# deprecated
*table_to_class = \&TableToClass;

sub AlzaboAttributes
{
    my $class = ref $_[0] || $_[0];

lib/Class/AlzaboWrapper.pm  view on Meta::CPAN

=item * Table()

This method returns the Alzabo table object associated with the
subclass.  This may also be called as an object method.

=item * AlzaboAttributes()

Returns a list of accessor methods that were created based on the
columns in the class's associated table.

=item * NewCursor ($cursor)

Given an C<Alzabo::Runtime::Cursor> object (either a row or join
cursor), this method returns a new C<Class::AlzaboWrapper::Cursor>
object.

=back

=head3 Object methods

=over 4

=item * row_object()

lib/Class/AlzaboWrapper.pm  view on Meta::CPAN

=item * TableToClass($table)

Given an Alzabo table object, this method returns its associated
subclass.

=back

=head3 Cursors

When using this module, you need to use the
C<Class::AlzaboWrapper::Cursor> module to wrap Alzabo's cursor
objects, so that objects the cursor returns are of the appropriate
subclass, not plain C<Alzabo::Runtime::Row> objects.  The C<Cursor()>
method provides some syntactic sugar for creating
C<Class::AlzaboWrapper::Cursor> objects.

=head3 Attributes created by subclasses

If you want to record the accessor methods your subclass makes so they
are available via C<AlzaboAttributes()>, you can call the
C<_RecordAttributeCreation()> method, which expects two arguments.
The first argument is the class for which the method was created and

lib/Class/AlzaboWrapper/Cursor.pm  view on Meta::CPAN

    ( on_fail =>
      sub { Class::AlzaboWrapper::Exception::Params->throw
                ( message => join '', @_ ) } );

sub new
{
    my $class = shift;

    my %p = validate_with( params => \@_,
                           spec =>
                           { cursor => { can => 'next' },
                             args   => { type => ARRAYREF, default => [] },
                             constructor_method => { type => SCALAR, default => 'new' },
                           },
                           allow_extra => 1,
                         );

    my $self = bless { %p,
                     }, $class;

    return $self;

lib/Class/AlzaboWrapper/Cursor.pm  view on Meta::CPAN

            );
}

sub next
{
    my $self = shift;

    my @things;
    if (wantarray)
    {
        @things = $self->{cursor}->next
            or return;
    }
    else
    {
        $things[0] = $self->{cursor}->next
            or return;
    }

    my @r = map { $self->_new_from_row($_) } @things;

    return wantarray ? @r : $r[0];
}

sub next_as_hash
{
    my $self = shift;

    my %hash = $self->{cursor}->next_as_hash or return;
    map { $hash{$_} = $self->_new_from_row($hash{$_}) } keys %hash;

    return %hash;
}

sub all
{
    my $self = shift;

    my @all;
    while ( my @a = $self->next )
    {
        push @all, @a == 1 ? $a[0] : \@a;
    }

    return @all;
}

sub count { $_[0]->{cursor}->count }


1;

__END__

=head1 NAME

Class::AlzaboWrapper::Cursor - Higher level wrapper around Alzabo cursor objects

=head1 SYNOPSIS

  my $cursor = Class::AlzaboWrapper::Cursor->new( cursor => $cursor );

=head1 DESCRIPTION

This module works with C<Class::AlzaboWrapper> to make sure that
objects returned from cursors are of the appropriate
C<Class::AlzaboWrapper> subclass, not raw C<Alzabo::Runtime::Cursor>
objects.

THIS MODULE IS STILL AN ALPHA RELEASE.  THE INTERFACE MAY CHANGE IN
FUTURE RELEASES.

=head1 USAGE

This module provides the following methods:

=over 4

=item * new

This method expects a C<cursor> parameter, which should be an
C<Alzabo::Runtime::Cursor> object.  This is the cursor being wrapped
by this class.

It also takes an "args" parameter.  This is an optional array
reference.  If given, then the arguments specified will be passed when
calling C<new()> for the appropriate subclass(es).

The "constructor_method" argument allows you to specify what method to
call when fetching the next object via C<next()>.  This defaults to
"new".

=item * next

This method is called to get the next object (or objects) in the
cursor.  Internally, it calls C<new()> on the appropriate
C<Class::AlzaboWrapper> subclass for each C<Alzabo::Runtime::Row>
object returned by the wrapped cursor.  It can be called in scalar or
array context, but in scalar context it will only return the first
object when there are more than one, so be careful.

=item * next_as_hash

Wrapper for the cursor's C<next_as_hash()> method, it behaves the same
as C<next> in that each row is returned as an appropriate
C<Class::AlzaboWrapper> subclass object.  Returns a hash keyed to the
table name(s).

=item * all

Returns an array containing all the remaining objects in the cursor.
If the wrapped cursor just returned single rows, then it returns an
array of objects.  Otherwise, it returns an array of array references,
with each reference containing one set of objects.

=item * count

A simple wrapper around the underlying cursor's C<count()> method.

=back

=head1 SUPPORT

The Alzabo docs are conveniently located online at
http://www.alzabo.org/docs/.

There is also a mailing list.  You can sign up at
http://lists.sourceforge.net/lists/listinfo/alzabo-general.



( run in 0.297 second using v1.01-cache-2.11-cpan-4d50c553e7e )