documentation improvements
[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
14
15 The name of the Catalyst model for this controller.
16
17 =cut
18
19 has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
20
21 =attribute_public result_class
22
23 Populates the result_class attribute of resultsets.
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 Returns the result_source of the stored_model.
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 Returns the model for the configured class.
46
47 Be aware that model is called as a class method on the Catalyst application
48 and not as an instance method on $c which might lead to unexpected results
49 in conjunction with ACCEPT_CONTEXT!
50
51 =cut
52
53 sub stored_model {
54     return $_[0]->_application->model( $_[0]->class );
55 }
56
57 =method_public check_has_column
58
59 Convenience method for checking if the column exists in the result source
60
61 =cut
62
63 sub check_has_column {
64     my ( $self, $col ) = @_;
65     die "Column '$col' does not exist in ResultSet '${\$self->class}'"
66         unless $self->stored_result_source->has_column($col);
67 }
68
69 =method_public check_has_relation
70
71 check_has_relation meticulously delves into the result sources relationships to
72 determine if the provided relation is valid.
73 Accepts a relation name, an optional HashRef indicating a nested relationship.
74 Iterates and recurses through provided arguments until exhausted.
75 Dies if at any time the relationship or column does not exist.
76
77 =cut
78
79 sub check_has_relation {
80     my ( $self, $rel, $other, $nest, $static ) = @_;
81
82     $nest ||= $self->stored_result_source;
83
84     if ( HashRef->check($other) ) {
85         my $rel_src = $nest->related_source($rel);
86         die "Relation '$rel_src' does not exist" if not defined($rel_src);
87
88         while ( my ( $k, $v ) = each %$other ) {
89             $self->check_has_relation( $k, $v, $rel_src, $static );
90         }
91     }
92     else {
93         return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*';
94         die "Relation '$rel' does not exist in ${\$nest->from}"
95             unless $nest->has_relationship($rel) || $nest->has_column($rel);
96         return 1;
97     }
98 }
99
100 =method_public check_column_relation
101
102 Convenience method to first check if the provided argument is a valid relation
103 (if it is a HashRef) or column.
104
105 =cut
106
107 sub check_column_relation {
108     my ( $self, $col_rel, $static ) = @_;
109
110     if ( HashRef->check($col_rel) ) {
111         try {
112             while ( my ( $k, $v ) = each %$col_rel ) {
113                 $self->check_has_relation( $k, $v, undef, $static );
114             }
115         }
116         catch {
117             # not a relation but a column with a predicate
118             while ( my ( $k, undef ) = each %$col_rel ) {
119                 $self->check_has_column($k);
120             }
121         }
122     }
123     else {
124         $self->check_has_column($col_rel);
125     }
126 }
127
128 1;