File Coverage

blib/lib/Dancer2/Plugin/DBIx/Class/ExportBuilder.pm
Criterion Covered Total %
statement 39 40 97.5
branch 5 8 62.5
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 55 60 91.6


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::DBIx::Class::ExportBuilder 1.06;
2 5     5   37 use Modern::Perl;
  5         14  
  5         32  
3 5     5   590 use Carp;
  5         12  
  5         288  
4 5     5   32 use Class::C3::Componentised;
  5         12  
  5         122  
5 5     5   3191 use curry;
  5         1642  
  5         145  
6 5     5   34 use Moo;
  5         11  
  5         30  
7              
8             has schema_class => ( is => 'ro', required => 1 );
9              
10             has dsn => ( is => 'ro', required => 1 );
11              
12             has user => ( is => 'ro' );
13              
14             has password => ( is => 'ro' );
15              
16             has schema => (
17             is => 'lazy',
18             builder => sub {
19 10     10   115 my ($self) = @_;
20 10         31 $self->_ensure_schema_class_loaded->connect( $self->dsn, $self->user,
21             $self->password );
22             },
23             );
24              
25             has export_prefix => ( is => 'ro' );
26              
27             sub _maybe_prefix_method {
28 36     36   461 my ( $self, $method ) = @_;
29 36 50       109 return $method unless $self->export_prefix;
30 0         0 return join( '_', $self->export_prefix, $method );
31             }
32              
33             sub _rs_name_methods {
34 10     10   34 my ($self) = @_;
35 10         38 my $class = $self->_ensure_schema_class_loaded;
36 10 100       136 return () unless $class->can('resultset_name_methods');
37 6         20 sort keys %{ $class->resultset_name_methods };
  6         144  
38             }
39              
40             sub _ensure_schema_class_loaded {
41 20 50   20   94 croak 'No schema class defined' if !$_[0]->schema_class;
42 20 50       48 eval {
43 20         116 Class::C3::Componentised->ensure_class_loaded( $_[0]->schema_class );
44 20         1793946 1;
45             }
46             or croak 'Schema class ' . $_[0]->schema_class . ' unable to load';
47 20         216 return $_[0]->schema_class;
48             }
49              
50             sub exports {
51 10     10 0 86 my ($self) = @_;
52 10         199 my $schema = $self->schema;
53 10         311491 my %kw;
54             ## no critic qw(Variables::ProhibitPackageVars)
55 10         126 $kw{$_} = $schema->$curry::curry($_) for $self->_rs_name_methods;
56             return map {
57 10         511 $self->_maybe_prefix_method($_) => do {
  36         88  
58 36         70 my $code = $kw{$_};
59 11     11   749994 sub { shift; &$code }
  11         62  
60 36         152 }
61             } sort keys %kw;
62             }
63              
64             1;
65              
66             __END__
67              
68             =pod
69              
70             =encoding UTF-8
71              
72             =head1 NAME
73              
74             Dancer2::Plugin::DBIx::Class::ExportBuilder
75              
76             =head1 VERSION
77              
78             version 1.06
79              
80             =head1 AUTHOR
81              
82             D Ruth Holloway <ruth@hiruthie.me>
83              
84             =head1 COPYRIGHT AND LICENSE
85              
86             This software is copyright (c) 2021 by D Ruth Holloway.
87              
88             This is free software; you can redistribute it and/or modify it under
89             the same terms as the Perl 5 programming language system itself.
90              
91             =cut