allow setting the result_class to undef to prevent the usage of HashRefInflator
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / StoredResultSource.pm
1 package Catalyst::Controller::DBIC::API::StoredResultSource;
2 #ABSTRACT: Provides accessors 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 result_class is: ro, isa: Str
21
22 result_class is the name of the resultset class that is the model for this controller
23
24 =cut
25
26 has 'result_class' => ( is => 'ro', isa => Maybe[Str], default => 'DBIx::Class::ResultClass::HashRefInflator' );
27
28 =method_public stored_result_source
29
30 This is the result source for the controller
31
32 =cut
33
34 sub stored_result_source
35 {
36     return shift->stored_model->result_source;
37 }
38
39 =method_public stored_model
40
41 This is the model for the controller
42
43 =cut
44
45 sub stored_model
46 {
47     return $_[0]->_application->model($_[0]->class);
48 }
49
50 =method_public check_has_column
51
52 Convenience method for checking if the column exists in the result source
53
54 =cut
55
56 sub 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
65 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.
66
67 =cut
68
69 sub check_has_relation
70 {
71     my ($self, $rel, $other, $nest, $static) = @_;
72
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
96 Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column.
97
98 =cut
99
100 sub check_column_relation
101 {
102     my ($self, $col_rel, $static) = @_;
103
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
128 1;