File Coverage

blib/lib/Mojo/DB/Role/ResultsRoles.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Mojo::DB::Role::ResultsRoles;
2              
3 1     1   745 use Mojo::Base -role;
  1         3  
  1         7  
4              
5             our $VERSION = 'v0.1.1';
6              
7             requires 'db';
8              
9             has results_roles => sub { [] };
10              
11             around db => sub {
12             my $orig = shift;
13             my $self = shift;
14             my $db = $self->$orig(@_);
15             my $class = $db->results_class;
16             my $roles = $self->results_roles;
17             $db->results_class($class->with_roles(@$roles)) if @$roles;
18             return $db;
19             };
20              
21             1;
22              
23             =head1 NAME
24              
25             Mojo::DB::Role::ResultsRoles - Apply roles to Mojo database results
26              
27             =head1 SYNOPSIS
28              
29             use Mojo::Pg;
30             my $pg = Mojo::Pg->new(...)->with_roles('Mojo::DB::Role::ResultsRoles');
31             push @{$pg->results_roles}, 'Mojo::DB::Results::Role::Something';
32             my $results = $pg->db->query(...);
33             # $results does Mojo::DB::Results::Role::Something
34              
35             =head1 DESCRIPTION
36              
37             This role allows roles to be applied to the results objects returned by
38             L or similar database APIs. The manager object must create database
39             connections via a C method, which must have a C attribute
40             used to instantiate results objects.
41              
42             =head1 ATTRIBUTES
43              
44             L composes the following attributes.
45              
46             =head2 results_roles
47              
48             my $roles = $manager->results_roles;
49             $manager = $manager->results_roles(\@roles);
50              
51             Array reference of roles to compose into results objects. This only affects
52             database objects created by subsequent calls to the C method.
53              
54             =head1 BUGS
55              
56             Report any issues on the public bugtracker.
57              
58             =head1 AUTHOR
59              
60             Dan Book
61              
62             =head1 COPYRIGHT AND LICENSE
63              
64             This software is Copyright (c) 2019 by Dan Book.
65              
66             This is free software, licensed under:
67              
68             The Artistic License 2.0 (GPL Compatible)
69              
70             =head1 SEE ALSO
71              
72             L, L, L