documentation improvements
[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
c0c8e1c6 13=attribute_public class
d2739840 14
c0c8e1c6 15The name of the Catalyst model for this controller.
d2739840 16
17=cut
18
19has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
20
c0c8e1c6 21=attribute_public result_class
a0a4ed30 22
c0c8e1c6 23Populates the result_class attribute of resultsets.
a0a4ed30 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
c0c8e1c6 35Returns the result_source of the stored_model.
d2739840 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
c0c8e1c6 45Returns the model for the configured class.
46
47Be aware that model is called as a class method on the Catalyst application
48and not as an instance method on $c which might lead to unexpected results
49in conjunction with ACCEPT_CONTEXT!
d2739840 50
51=cut
52
8ea592cb 53sub stored_model {
54 return $_[0]->_application->model( $_[0]->class );
d2739840 55}
56
d2739840 57=method_public check_has_column
58
59Convenience method for checking if the column exists in the result source
60
61=cut
62
8ea592cb 63sub check_has_column {
64 my ( $self, $col ) = @_;
d2739840 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
8ea592cb 71check_has_relation meticulously delves into the result sources relationships to
72determine if the provided relation is valid.
73Accepts a relation name, an optional HashRef indicating a nested relationship.
74Iterates and recurses through provided arguments until exhausted.
75Dies if at any time the relationship or column does not exist.
d2739840 76
77=cut
78
8ea592cb 79sub check_has_relation {
80 my ( $self, $rel, $other, $nest, $static ) = @_;
406086f3 81
d2739840 82 $nest ||= $self->stored_result_source;
83
8ea592cb 84 if ( HashRef->check($other) ) {
d2739840 85 my $rel_src = $nest->related_source($rel);
86 die "Relation '$rel_src' does not exist" if not defined($rel_src);
87
8ea592cb 88 while ( my ( $k, $v ) = each %$other ) {
89 $self->check_has_relation( $k, $v, $rel_src, $static );
d2739840 90 }
91 }
8ea592cb 92 else {
d2739840 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
8ea592cb 102Convenience method to first check if the provided argument is a valid relation
103(if it is a HashRef) or column.
d2739840 104
105=cut
106
8ea592cb 107sub check_column_relation {
108 my ( $self, $col_rel, $static ) = @_;
109
110 if ( HashRef->check($col_rel) ) {
111 try {
112 while ( my ( $k, $v ) = each %$col_rel ) {
113 $self->check_has_relation( $k, $v, undef, $static );
d2739840 114 }
115 }
8ea592cb 116 catch {
d2739840 117 # not a relation but a column with a predicate
8ea592cb 118 while ( my ( $k, undef ) = each %$col_rel ) {
d2739840 119 $self->check_has_column($k);
120 }
121 }
122 }
8ea592cb 123 else {
d2739840 124 $self->check_has_column($col_rel);
125 }
126}
127
1281;