1 package Catalyst::Controller::DBIC::API::StoredResultSource;
3 #ABSTRACT: Provides accessors for static resources
6 use MooseX::Types::Moose(':all');
7 use Catalyst::Controller::DBIC::API::Types(':all');
9 use namespace::autoclean;
11 requires '_application';
13 =attribute_public class is: ro, isa: Str
15 class is the name of the class that is the model for this controller
19 has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
21 =attribute_public result_class is: ro, isa: Str
23 result_class is the name of the resultset class that is the model for this controller
27 has 'result_class' => (
30 default => 'DBIx::Class::ResultClass::HashRefInflator'
33 =method_public stored_result_source
35 This is the result source for the controller
39 sub stored_result_source {
40 return shift->stored_model->result_source;
43 =method_public stored_model
45 This is the model for the controller
50 return $_[0]->_application->model( $_[0]->class );
53 =method_public check_has_column
55 Convenience method for checking if the column exists in the result source
59 sub check_has_column {
60 my ( $self, $col ) = @_;
61 die "Column '$col' does not exist in ResultSet '${\$self->class}'"
62 unless $self->stored_result_source->has_column($col);
65 =method_public check_has_relation
67 check_has_relation meticulously delves into the result sources relationships to
68 determine if the provided relation is valid.
69 Accepts a relation name, an optional HashRef indicating a nested relationship.
70 Iterates and recurses through provided arguments until exhausted.
71 Dies if at any time the relationship or column does not exist.
75 sub check_has_relation {
76 my ( $self, $rel, $other, $nest, $static ) = @_;
78 $nest ||= $self->stored_result_source;
80 if ( HashRef->check($other) ) {
81 my $rel_src = $nest->related_source($rel);
82 die "Relation '$rel_src' does not exist" if not defined($rel_src);
84 while ( my ( $k, $v ) = each %$other ) {
85 $self->check_has_relation( $k, $v, $rel_src, $static );
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);
96 =method_public check_column_relation
98 Convenience method to first check if the provided argument is a valid relation
99 (if it is a HashRef) or column.
103 sub check_column_relation {
104 my ( $self, $col_rel, $static ) = @_;
106 if ( HashRef->check($col_rel) ) {
108 while ( my ( $k, $v ) = each %$col_rel ) {
109 $self->check_has_relation( $k, $v, undef, $static );
113 # not a relation but a column with a predicate
114 while ( my ( $k, undef ) = each %$col_rel ) {
115 $self->check_has_column($k);
120 $self->check_has_column($col_rel);