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
15 The name of the Catalyst model for this controller.
19 has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
21 =attribute_public result_class
23 Populates the result_class attribute of resultsets.
27 has 'result_class' => (
30 default => 'DBIx::Class::ResultClass::HashRefInflator'
33 =method_public stored_result_source
35 Returns the result_source of the stored_model.
39 sub stored_result_source {
40 return shift->stored_model->result_source;
43 =method_public stored_model
45 Returns the model for the configured class.
47 Be aware that model is called as a class method on the Catalyst application
48 and not as an instance method on $c which might lead to unexpected results
49 in conjunction with ACCEPT_CONTEXT!
54 return $_[0]->_application->model( $_[0]->class );
57 =method_public check_has_column
59 Convenience method for checking if the column exists in the result source
63 sub check_has_column {
64 my ( $self, $col ) = @_;
65 die "Column '$col' does not exist in ResultSet '${\$self->class}'"
66 unless $self->stored_result_source->has_column($col);
69 =method_public check_has_relation
71 check_has_relation meticulously delves into the result sources relationships to
72 determine if the provided relation is valid.
73 Accepts a relation name, an optional HashRef indicating a nested relationship.
74 Iterates and recurses through provided arguments until exhausted.
75 Dies if at any time the relationship or column does not exist.
79 sub check_has_relation {
80 my ( $self, $rel, $other, $nest, $static ) = @_;
82 $nest ||= $self->stored_result_source;
84 if ( HashRef->check($other) ) {
85 my $rel_src = $nest->related_source($rel);
86 die "Relation '$rel_src' does not exist" if not defined($rel_src);
88 while ( my ( $k, $v ) = each %$other ) {
89 $self->check_has_relation( $k, $v, $rel_src, $static );
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);
100 =method_public check_column_relation
102 Convenience method to first check if the provided argument is a valid relation
103 (if it is a HashRef) or column.
107 sub check_column_relation {
108 my ( $self, $col_rel, $static ) = @_;
110 if ( HashRef->check($col_rel) ) {
112 while ( my ( $k, $v ) = each %$col_rel ) {
113 $self->check_has_relation( $k, $v, undef, $static );
117 # not a relation but a column with a predicate
118 while ( my ( $k, undef ) = each %$col_rel ) {
119 $self->check_has_column($k);
124 $self->check_has_column($col_rel);