File Coverage

blib/lib/Catalyst/Controller/DBIC/API/StoredResultSource.pm
Criterion Covered Total %
statement 38 39 97.4
branch 11 12 91.6
condition 10 12 83.3
subroutine 12 12 100.0
pod 5 5 100.0
total 76 80 95.0


line stmt bran cond sub pod time code
1             package Catalyst::Controller::DBIC::API::StoredResultSource;
2             $Catalyst::Controller::DBIC::API::StoredResultSource::VERSION = '2.008001';
3             #ABSTRACT: Provides accessors for static resources
4              
5 16     16   11550 use Moose::Role;
  16         51  
  16         121  
6 16     16   81779 use MooseX::Types::Moose(':all');
  16         44  
  16         137  
7 16     16   141781 use Catalyst::Controller::DBIC::API::Types(':all');
  16         81  
  16         117  
8 16     16   85442 use Try::Tiny;
  16         47  
  16         1227  
9 16     16   140 use namespace::autoclean;
  16         47  
  16         180  
10              
11             requires '_application';
12              
13              
14             has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
15              
16              
17             has 'result_class' => (
18             is => 'ro',
19             isa => Maybe [Str],
20             default => 'DBIx::Class::ResultClass::HashRefInflator'
21             );
22              
23              
24             sub stored_result_source {
25 693     693 1 2512 return shift->stored_model->result_source;
26             }
27              
28              
29             sub stored_model {
30 693     693 1 22449 return $_[0]->_application->model( $_[0]->class );
31             }
32              
33              
34             sub check_has_column {
35 571     571 1 1519 my ( $self, $col ) = @_;
36 571 100       1966 die "Column '$col' does not exist in ResultSet '${\$self->class}'"
  1         2345  
37             unless $self->stored_result_source->has_column($col);
38             }
39              
40              
41             sub check_has_relation {
42 77     77 1 294 my ( $self, $rel, $other, $nest, $static ) = @_;
43              
44 77   66     478 $nest ||= $self->stored_result_source;
45              
46 77 100       39887 if ( HashRef->check($other) ) {
47 20         13555 my $rel_src = $nest->related_source($rel);
48 13 50       1906 die "Relation '$rel_src' does not exist" if not defined($rel_src);
49              
50 13         103 while ( my ( $k, $v ) = each %$other ) {
51 13         71 $self->check_has_relation( $k, $v, $rel_src, $static );
52             }
53             }
54             else {
55 57 100 100     39484 return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*';
      66        
56 27 100 100     2106 die "Relation '$rel' does not exist in ${\$nest->from}"
  8         194  
57             unless $nest->has_relationship($rel) || $nest->has_column($rel);
58 19         484 return 1;
59             }
60             }
61              
62              
63             sub check_column_relation {
64 632     632 1 167711 my ( $self, $col_rel, $static ) = @_;
65              
66 632 100       2263 if ( HashRef->check($col_rel) ) {
67             try {
68 61     61   3366 while ( my ( $k, $v ) = each %$col_rel ) {
69 61         471 $self->check_has_relation( $k, $v, undef, $static );
70             }
71             }
72             catch {
73             # not a relation but a column with a predicate
74 15     15   6229 while ( my ( $k, undef ) = each %$col_rel ) {
75 0         0 $self->check_has_column($k);
76             }
77             }
78 61         43020 }
79             else {
80 571         388758 $self->check_has_column($col_rel);
81             }
82             }
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =head1 NAME
91              
92             Catalyst::Controller::DBIC::API::StoredResultSource - Provides accessors for static resources
93              
94             =head1 VERSION
95              
96             version 2.008001
97              
98             =head1 PUBLIC_ATTRIBUTES
99              
100             =head2 class
101              
102             The name of the Catalyst model for this controller.
103              
104             =head2 result_class
105              
106             Populates the result_class attribute of resultsets.
107              
108             =head1 PUBLIC_METHODS
109              
110             =head2 stored_result_source
111              
112             Returns the result_source of the stored_model.
113              
114             =head2 stored_model
115              
116             Returns the model for the configured class.
117              
118             Be aware that model is called as a class method on the Catalyst application
119             and not as an instance method on $c which might lead to unexpected results
120             in conjunction with ACCEPT_CONTEXT!
121              
122             =head2 check_has_column
123              
124             Convenience method for checking if the column exists in the result source
125              
126             =head2 check_has_relation
127              
128             check_has_relation meticulously delves into the result sources relationships to
129             determine if the provided relation is valid.
130             Accepts a relation name, an optional HashRef indicating a nested relationship.
131             Iterates and recurses through provided arguments until exhausted.
132             Dies if at any time the relationship or column does not exist.
133              
134             =head2 check_column_relation
135              
136             Convenience method to first check if the provided argument is a valid relation
137             (if it is a HashRef) or column.
138              
139             =head1 AUTHORS
140              
141             =over 4
142              
143             =item *
144              
145             Nicholas Perez <nperez@cpan.org>
146              
147             =item *
148              
149             Luke Saunders <luke.saunders@gmail.com>
150              
151             =item *
152              
153             Alexander Hartmaier <abraxxa@cpan.org>
154              
155             =item *
156              
157             Florian Ragwitz <rafl@debian.org>
158              
159             =item *
160              
161             Oleg Kostyuk <cub.uanic@gmail.com>
162              
163             =item *
164              
165             Samuel Kaufman <sam@socialflow.com>
166              
167             =back
168              
169             =head1 COPYRIGHT AND LICENSE
170              
171             This software is copyright (c) 2019 by Luke Saunders, Nicholas Perez, Alexander Hartmaier, et al.
172              
173             This is free software; you can redistribute it and/or modify it under
174             the same terms as the Perl 5 programming language system itself.
175              
176             =cut