Class-DBI-ViewLoader

 view release on metacpan or  search on metacpan

lib/Class/DBI/ViewLoader/Auto.pm  view on Meta::CPAN


=head1 METHODS

=head2 load_views

    $loader = $cdbi_class->load_views( %opts or $include )

Loads views from the database connection in $cdbi_class.

The default namespace is the same as the calling class.

%opts is passed to the Class::DBI::Loader constructor. If a scalar argument is
given instead of a hash or hashref, it is interpreted as being the include
pattern.

The options dsn, username, password and options are silently ignored.

$cdbi_class should always be the leftmost base class of the generated classes.
base_classes and left_base_classes options are supported, but it might make more
sense to add those bases to the calling class manually.

Returns the same as Class::DBI::ViewLoader->load_views, i.e. a list of loaded
classes.

=cut

# Class::DBI::ViewLoader options to ignore
my @unsupported = qw( dsn username password options );

sub load_views {
    my $class = shift;
    my %opts;

    if (@_ == 1) {
        my $proto = shift;
        if (ref $proto eq 'HASH') {
            %opts = %$proto;
        }
        else {
            $opts{'include'} = $proto;
        }
    }
    else {
        %opts = @_;
    }

    delete @opts{@unsupported};
    $opts{'namespace'} ||= $class;

    Class::DBI::ViewLoader->_compat(\%opts);

    my $sub = $class->can('db_Main')
        or croak "$class has no connection";

    my $dbh = &$sub($class);
    my $driver = $dbh->{'Driver'}->{'Name'};

    return Class::DBI::ViewLoader->new(%opts)
                                 ->_load_driver($driver)
                                 ->_set_dbi_handle($dbh)
                                 ->_set_keepalive(1)
                                 ->add_left_base_classes($class)
                                 ->load_views;
}

1;

__END__

=head1 DIAGNOSTICS

=head2 %s has no connection

The given class had no connection set up to read views from.

=cut

vim: ts=8 sts=4 sw=4 noet sr



( run in 1.448 second using v1.01-cache-2.11-cpan-39bf76dae61 )