whitespace clean up
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / StoredResultSource.pm
1 package Catalyst::Controller::DBIC::API::StoredResultSource;
2 #ABSTRACT: Provides acessors for static resources
3
4 use Moose::Role;
5 use MooseX::Types::Moose(':all');
6 use Catalyst::Controller::DBIC::API::Types(':all');
7 use Try::Tiny;
8 use namespace::autoclean;
9
10 requires '_application';
11
12 =attribute_public class is: ro, isa: Str
13
14 class is the name of the class that is the model for this controller
15
16 =cut
17
18 has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
19
20 =attribute_public stored_result_source is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/ResultSource>
21
22 This is the result source for the controller
23
24 =cut
25
26 has 'stored_result_source' =>
27 (
28     is => 'ro',
29     isa => ResultSource,
30     lazy_build => 1,
31 );
32
33 =attribute_public stored_model is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/Model>
34
35 This is the model for the controller
36
37 =cut
38
39 has 'stored_model' =>
40 (
41     is => 'ro',
42     isa => Model,
43     lazy_build => 1,
44 );
45
46 sub _build_stored_model
47 {
48     return $_[0]->_application->model($_[0]->class);
49 }
50
51 sub _build_stored_result_source
52 {
53     return shift->stored_model->result_source();
54 }
55
56 =method_public check_has_column
57
58 Convenience method for checking if the column exists in the result source
59
60 =cut
61
62 sub check_has_column
63 {
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 determine if the provided relation is valid. Accepts a relation name, and optional HashRef indicating a nested relationship. Iterates, and recurses through provided arguments until exhausted. Dies if at any time the relationship or column does not exist.
72
73 =cut
74
75 sub check_has_relation
76 {
77     my ($self, $rel, $other, $nest, $static) = @_;
78
79     $nest ||= $self->stored_result_source;
80
81     if(HashRef->check($other))
82     {
83         my $rel_src = $nest->related_source($rel);
84         die "Relation '$rel_src' does not exist" if not defined($rel_src);
85
86         while(my($k,$v) = each %$other)
87         {
88             $self->check_has_relation($k, $v, $rel_src, $static);
89         }
90     }
91     else
92     {
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 (if it is a HashRef) or column.
103
104 =cut
105
106 sub check_column_relation
107 {
108     my ($self, $col_rel, $static) = @_;
109
110     if(HashRef->check($col_rel))
111     {
112         try
113         {
114             while(my($k,$v) = each %$col_rel)
115             {
116                 $self->check_has_relation($k, $v, undef, $static);
117             }
118         }
119         catch
120         {
121             # not a relation but a column with a predicate
122             while(my($k, undef) = each %$col_rel)
123             {
124                 $self->check_has_column($k);
125             }
126         }
127     }
128     else
129     {
130         $self->check_has_column($col_rel);
131     }
132 }
133
134 1;