code cleanups
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / StoredResultSource.pm
CommitLineData
d2739840 1package Catalyst::Controller::DBIC::API::StoredResultSource;
d72e5927 2#ABSTRACT: Provides accessors for static resources
d2739840 3
4use Moose::Role;
5use MooseX::Types::Moose(':all');
6use Catalyst::Controller::DBIC::API::Types(':all');
7use Try::Tiny;
8use namespace::autoclean;
9
10requires '_application';
11
12=attribute_public class is: ro, isa: Str
13
14class is the name of the class that is the model for this controller
15
16=cut
17
18has '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
22This is the result source for the controller
23
24=cut
25
26has '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
35This is the model for the controller
36
37=cut
38
39has 'stored_model' =>
40(
41 is => 'ro',
42 isa => Model,
43 lazy_build => 1,
44);
45
46sub _build_stored_model
406086f3 47{
d2739840 48 return $_[0]->_application->model($_[0]->class);
49}
50
51sub _build_stored_result_source
52{
53 return shift->stored_model->result_source();
54}
55
56=method_public check_has_column
57
58Convenience method for checking if the column exists in the result source
59
60=cut
61
62sub 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
71check_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
75sub check_has_relation
76{
77 my ($self, $rel, $other, $nest, $static) = @_;
406086f3 78
d2739840 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
406086f3 102Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column.
d2739840 103
104=cut
105
106sub check_column_relation
107{
108 my ($self, $col_rel, $static) = @_;
406086f3 109
d2739840 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
1341;