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