File Coverage

blib/lib/Rose/DBx/Role/QueryCannery.pm
Criterion Covered Total %
statement 23 24 95.8
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 33 35 94.2


line stmt bran cond sub pod time code
1             #!perl
2             #
3             # $Id$
4              
5 3     3   818465 use strict;
  3         7  
  3         70  
6 3     3   14 use warnings;
  3         6  
  3         146  
7              
8             package Rose::DBx::Role::QueryCannery;
9              
10             our ($VERSION) = '1.00';
11              
12 3     3   729 use Module::Runtime qw( use_module );
  3         1531  
  3         20  
13 3     3   2163 use MooX::Role::Parameterized;
  3         4970  
  3         216  
14              
15             sub _load_if {
16 5     5   9 my $pkg = shift;
17 3     3   17 no strict 'refs';
  3         4  
  3         176  
18 5 50       7 return $pkg if keys %{ $pkg . '::' };
  5         34  
19 0         0 use_module($pkg);
20             }
21              
22 3     3   1396 use Moo::Role 2;
  3         37659  
  3         21  
23              
24             role {
25             my $params = shift;
26              
27             my ( $canning_class, %datasource );
28              
29             if ( $params->{query_class} ) {
30             $canning_class = _load_if( $params->{query_class} );
31             }
32             else {
33             # We know these are in their own files, so we can use_module directly
34             $canning_class =
35             eval { use_module('Rose::DBx::CannedQuery::Glycosylated') }
36             || use_module('Rose::DBx::CannedQuery');
37             }
38              
39             foreach my $key (qw/ rdb rdb_class rdb_params /) {
40             next unless $params->{$key};
41             $datasource{$key} = $params->{$key};
42             _load_if( $params->{$key} ) if $key eq 'rdb_class';
43             }
44              
45             my $log_args = sub { return (); };
46             if ( $canning_class->can('verbose') and $canning_class->can('logger') ) {
47             $log_args = sub {
48             my $consumer = shift;
49             my (@args);
50             push @args, verbose => $consumer->verbose || 0
51             if $consumer->can('verbose');
52             push @args, logger => $consumer->logger
53             if $consumer->can('logger') && $consumer->logger;
54             return @args;
55             };
56             } ## end if ( $canning_class->can...)
57              
58             my $create_query = sub {
59             my ( $constructor, $obj, $sql, $opts ) = @_;
60             $opts ||= {};
61             my (%merged_args) = (
62             $log_args->($obj),
63             %datasource,
64             sql => $sql,
65             %$opts
66             );
67             return $canning_class->$constructor(%merged_args);
68             };
69              
70 4     4   13729 method 'build_query' => sub { $create_query->( 'new', @_ ); };
71 3     3   40845 method 'get_query' => sub { $create_query->( 'new_or_cached', @_ ); };
72              
73             };
74              
75             1;
76              
77             __END__