1 package Catalyst::Controller::DBIC::API::StoredResultSource;
2 #ABSTRACT: Provides accessors for static resources
5 use MooseX::Types::Moose(':all');
6 use Catalyst::Controller::DBIC::API::Types(':all');
8 use namespace::autoclean;
10 requires '_application';
12 =attribute_public class is: ro, isa: Str
14 class is the name of the class that is the model for this controller
18 has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' );
20 =attribute_public stored_result_source is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/ResultSource>
22 This is the result source for the controller
26 has 'stored_result_source' =>
33 =attribute_public stored_model is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/Model>
35 This is the model for the controller
46 sub _build_stored_model
48 return $_[0]->_application->model($_[0]->class);
51 sub _build_stored_result_source
53 return shift->stored_model->result_source();
56 =method_public check_has_column
58 Convenience method for checking if the column exists in the result source
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 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.
75 sub check_has_relation
77 my ($self, $rel, $other, $nest, $static) = @_;
79 $nest ||= $self->stored_result_source;
81 if(HashRef->check($other))
83 my $rel_src = $nest->related_source($rel);
84 die "Relation '$rel_src' does not exist" if not defined($rel_src);
86 while(my($k,$v) = each %$other)
88 $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 (if it is a HashRef) or column.
106 sub check_column_relation
108 my ($self, $col_rel, $static) = @_;
110 if(HashRef->check($col_rel))
114 while(my($k,$v) = each %$col_rel)
116 $self->check_has_relation($k, $v, undef, $static);
121 # not a relation but a column with a predicate
122 while(my($k, undef) = each %$col_rel)
124 $self->check_has_column($k);
130 $self->check_has_column($col_rel);