perltidy all classes
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / StoredResultSource.pm
1 package Catalyst::Controller::DBIC::API::StoredResultSource;
2
3 #ABSTRACT: Provides accessors for static resources
4
5 use Moose::Role;
6 use MooseX::Types::Moose(':all');
7 use Catalyst::Controller::DBIC::API::Types(':all');
8 use Try::Tiny;
9 use namespace::autoclean;
10
11 requires '_application';
12
13 =attribute_public class is: ro, isa: Str
14
15 class is the name of the class that is the model for this controller
16
17 =cut
18
19 has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
20
21 =attribute_public result_class is: ro, isa: Str
22
23 result_class is the name of the resultset class that is the model for this controller
24
25 =cut
26
27 has 'result_class' => (
28     is      => 'ro',
29     isa     => Maybe [Str],
30     default => 'DBIx::Class::ResultClass::HashRefInflator'
31 );
32
33 =method_public stored_result_source
34
35 This is the result source for the controller
36
37 =cut
38
39 sub stored_result_source {
40     return shift->stored_model->result_source;
41 }
42
43 =method_public stored_model
44
45 This is the model for the controller
46
47 =cut
48
49 sub stored_model {
50     return $_[0]->_application->model( $_[0]->class );
51 }
52
53 =method_public check_has_column
54
55 Convenience method for checking if the column exists in the result source
56
57 =cut
58
59 sub check_has_column {
60     my ( $self, $col ) = @_;
61     die "Column '$col' does not exist in ResultSet '${\$self->class}'"
62         unless $self->stored_result_source->has_column($col);
63 }
64
65 =method_public check_has_relation
66
67 check_has_relation meticulously delves into the result sources relationships to
68 determine if the provided relation is valid.
69 Accepts a relation name, an optional HashRef indicating a nested relationship.
70 Iterates and recurses through provided arguments until exhausted.
71 Dies if at any time the relationship or column does not exist.
72
73 =cut
74
75 sub check_has_relation {
76     my ( $self, $rel, $other, $nest, $static ) = @_;
77
78     $nest ||= $self->stored_result_source;
79
80     if ( HashRef->check($other) ) {
81         my $rel_src = $nest->related_source($rel);
82         die "Relation '$rel_src' does not exist" if not defined($rel_src);
83
84         while ( my ( $k, $v ) = each %$other ) {
85             $self->check_has_relation( $k, $v, $rel_src, $static );
86         }
87     }
88     else {
89         return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*';
90         die "Relation '$rel' does not exist in ${\$nest->from}"
91             unless $nest->has_relationship($rel) || $nest->has_column($rel);
92         return 1;
93     }
94 }
95
96 =method_public check_column_relation
97
98 Convenience method to first check if the provided argument is a valid relation
99 (if it is a HashRef) or column.
100
101 =cut
102
103 sub check_column_relation {
104     my ( $self, $col_rel, $static ) = @_;
105
106     if ( HashRef->check($col_rel) ) {
107         try {
108             while ( my ( $k, $v ) = each %$col_rel ) {
109                 $self->check_has_relation( $k, $v, undef, $static );
110             }
111         }
112         catch {
113             # not a relation but a column with a predicate
114             while ( my ( $k, undef ) = each %$col_rel ) {
115                 $self->check_has_column($k);
116             }
117         }
118     }
119     else {
120         $self->check_has_column($col_rel);
121     }
122 }
123
124 1;