Commit | Line | Data |
d2739840 |
1 | package Catalyst::Controller::DBIC::API::StoredResultSource; |
d72e5927 |
2 | #ABSTRACT: Provides accessors for static resources |
d2739840 |
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 | |
a0a4ed30 |
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 => Str, default => 'DBIx::Class::ResultClass::HashRefInflator' ); |
27 | |
d2739840 |
28 | =attribute_public stored_result_source is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/ResultSource> |
29 | |
30 | This is the result source for the controller |
31 | |
32 | =cut |
33 | |
34 | has 'stored_result_source' => |
35 | ( |
36 | is => 'ro', |
37 | isa => ResultSource, |
38 | lazy_build => 1, |
39 | ); |
40 | |
41 | =attribute_public stored_model is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/Model> |
42 | |
43 | This is the model for the controller |
44 | |
45 | =cut |
46 | |
47 | has 'stored_model' => |
48 | ( |
49 | is => 'ro', |
50 | isa => Model, |
51 | lazy_build => 1, |
52 | ); |
53 | |
54 | sub _build_stored_model |
406086f3 |
55 | { |
d2739840 |
56 | return $_[0]->_application->model($_[0]->class); |
57 | } |
58 | |
59 | sub _build_stored_result_source |
60 | { |
61 | return shift->stored_model->result_source(); |
62 | } |
63 | |
64 | =method_public check_has_column |
65 | |
66 | Convenience method for checking if the column exists in the result source |
67 | |
68 | =cut |
69 | |
70 | sub check_has_column |
71 | { |
72 | my ($self, $col) = @_; |
73 | die "Column '$col' does not exist in ResultSet '${\$self->class}'" |
74 | unless $self->stored_result_source->has_column($col); |
75 | } |
76 | |
77 | =method_public check_has_relation |
78 | |
79 | 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. |
80 | |
81 | =cut |
82 | |
83 | sub check_has_relation |
84 | { |
85 | my ($self, $rel, $other, $nest, $static) = @_; |
406086f3 |
86 | |
d2739840 |
87 | $nest ||= $self->stored_result_source; |
88 | |
89 | if(HashRef->check($other)) |
90 | { |
91 | my $rel_src = $nest->related_source($rel); |
92 | die "Relation '$rel_src' does not exist" if not defined($rel_src); |
93 | |
94 | while(my($k,$v) = each %$other) |
95 | { |
96 | $self->check_has_relation($k, $v, $rel_src, $static); |
97 | } |
98 | } |
99 | else |
100 | { |
101 | return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*'; |
102 | die "Relation '$rel' does not exist in ${\$nest->from}" |
103 | unless $nest->has_relationship($rel) || $nest->has_column($rel); |
104 | return 1; |
105 | } |
106 | } |
107 | |
108 | =method_public check_column_relation |
109 | |
406086f3 |
110 | Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column. |
d2739840 |
111 | |
112 | =cut |
113 | |
114 | sub check_column_relation |
115 | { |
116 | my ($self, $col_rel, $static) = @_; |
406086f3 |
117 | |
d2739840 |
118 | if(HashRef->check($col_rel)) |
119 | { |
120 | try |
121 | { |
122 | while(my($k,$v) = each %$col_rel) |
123 | { |
124 | $self->check_has_relation($k, $v, undef, $static); |
125 | } |
126 | } |
127 | catch |
128 | { |
129 | # not a relation but a column with a predicate |
130 | while(my($k, undef) = each %$col_rel) |
131 | { |
132 | $self->check_has_column($k); |
133 | } |
134 | } |
135 | } |
136 | else |
137 | { |
138 | $self->check_has_column($col_rel); |
139 | } |
140 | } |
141 | |
142 | 1; |