PrePAN

Sign in to PrePAN

PGObject::Simple::Role Moo/Moose role implementing PGObject::Simple interfaces

Author
einhverfr@github
Date
URL
Status
Shipped
Good

Synopsis

Take the following (Moose) class:

    package MyAPP::Foo;
    use Moose;
    with 'PGObject::Simple::Role';

    has id  => (is => 'ro', isa => 'Int', required => 0);
    has foo => (is => 'ro', isa => 'Str', required => 0);
    has bar => (is => 'ro', isa => 'Str', required => 0);
    has baz => (is => 'ro', isa => 'Int', required => 0);

    sub get_dbh {
        return DBI->connect('dbi:Pg:dbname=foobar');
    }

And a stored procedure: 

    CREATE OR REPLACE FUNCTION foo_to_int
    (in_id int, in_foo text, in_bar text, in_baz int)
    RETURNS INT LANGUAGE SQL AS
    $$
    select char_length($2) + char_length($3) + $1 * $4;
    $$;

Then the following Perl code would work to invoke it:

    my $foobar = MyApp::Foo->new(id => 3, foo => 'foo', bar => 'baz', baz => 33);
    $foobar->call_dbmethod(funcname => 'foo_to_int');

The full interface of call_dbmethod and call_procedure from PGObject::Simple are
supported.

Description

PGObject::Simple::Role arose out of a need for more formalized object structures than the quick and dirty approach (released in PGObject::Simple) required. PGObject::Simple::Role thus gives you the quick and flexible mapping of stored procedures to application objects as a Moose role, giving you the power of that object system as well.

This module implements essentially a wrapper around PGObject::Simple. The internals are not the same. Both the enumerated argument (call_procedure) and mapped argument (call_dbmethod) APIs are supported.

This is based on code developed for the forthcoming LedgerSMB 1.4. It is thus not an approach we have used extensively in production for many users yet, but it will get there.

Comments

anonymouse
Anonymous
What about making this out of Moo, instead of Moose?
We originally wrote the code with Moose but in the process of reworking for CPAN, I think Moo may be something to use. One thing I am struggling a little bit with in the docs is to figure out how exactly the isa => CODE maps to the Moose has block.
anonymouse
Anonymous
Perhaps look at MooX::Types::MooseLike
Still looking at it. It looks like I can just declare a role with isa => CODE and have it work from a Moose class too right? If so, I think changing to moo will be pretty straightforward.
anonymouse
Anonymous
You can, Type::Tiny and MooX::Types just make those coderefs for you
Got it working with Moo, released. Thanks :-)

Please sign up to post a review.