Changed stored_model and stored_result_source attributes to methods for ACCEPT_CONTEX...
[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
a0a4ed30 20=attribute_public result_class is: ro, isa: Str
21
22result_class is the name of the resultset class that is the model for this controller
23
24=cut
25
26has 'result_class' => ( is => 'ro', isa => Str, default => 'DBIx::Class::ResultClass::HashRefInflator' );
27
362e979f 28=method_public stored_result_source
d2739840 29
30This is the result source for the controller
31
32=cut
33
362e979f 34sub stored_result_source
35{
36 return shift->stored_model->result_source;
37}
d2739840 38
362e979f 39=method_public stored_model
d2739840 40
41This is the model for the controller
42
43=cut
44
362e979f 45sub stored_model
406086f3 46{
d2739840 47 return $_[0]->_application->model($_[0]->class);
48}
49
d2739840 50=method_public check_has_column
51
52Convenience method for checking if the column exists in the result source
53
54=cut
55
56sub check_has_column
57{
58 my ($self, $col) = @_;
59 die "Column '$col' does not exist in ResultSet '${\$self->class}'"
60 unless $self->stored_result_source->has_column($col);
61}
62
63=method_public check_has_relation
64
65check_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.
66
67=cut
68
69sub check_has_relation
70{
71 my ($self, $rel, $other, $nest, $static) = @_;
406086f3 72
d2739840 73 $nest ||= $self->stored_result_source;
74
75 if(HashRef->check($other))
76 {
77 my $rel_src = $nest->related_source($rel);
78 die "Relation '$rel_src' does not exist" if not defined($rel_src);
79
80 while(my($k,$v) = each %$other)
81 {
82 $self->check_has_relation($k, $v, $rel_src, $static);
83 }
84 }
85 else
86 {
87 return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*';
88 die "Relation '$rel' does not exist in ${\$nest->from}"
89 unless $nest->has_relationship($rel) || $nest->has_column($rel);
90 return 1;
91 }
92}
93
94=method_public check_column_relation
95
406086f3 96Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column.
d2739840 97
98=cut
99
100sub check_column_relation
101{
102 my ($self, $col_rel, $static) = @_;
406086f3 103
d2739840 104 if(HashRef->check($col_rel))
105 {
106 try
107 {
108 while(my($k,$v) = each %$col_rel)
109 {
110 $self->check_has_relation($k, $v, undef, $static);
111 }
112 }
113 catch
114 {
115 # not a relation but a column with a predicate
116 while(my($k, undef) = each %$col_rel)
117 {
118 $self->check_has_column($k);
119 }
120 }
121 }
122 else
123 {
124 $self->check_has_column($col_rel);
125 }
126}
127
1281;