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 result_class is: ro, isa: Str
22 result_class is the name of the resultset class that is the model for this controller
26 has 'result_class' => ( is => 'ro', isa => Str, default => 'DBIx::Class::ResultClass::HashRefInflator' );
28 =attribute_public stored_result_source is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/ResultSource>
30 This is the result source for the controller
34 has 'stored_result_source' =>
41 =attribute_public stored_model is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/Model>
43 This is the model for the controller
54 sub _build_stored_model
56 return $_[0]->_application->model($_[0]->class);
59 sub _build_stored_result_source
61 return shift->stored_model->result_source();
64 =method_public check_has_column
66 Convenience method for checking if the column exists in the result source
72 my ($self, $col) = @_;
73 die "Column '$col' does not exist in ResultSet '${\$self->class}'"
74 unless $self->stored_result_source->has_column($col);
77 =method_public check_has_relation
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.
83 sub check_has_relation
85 my ($self, $rel, $other, $nest, $static) = @_;
87 $nest ||= $self->stored_result_source;
89 if(HashRef->check($other))
91 my $rel_src = $nest->related_source($rel);
92 die "Relation '$rel_src' does not exist" if not defined($rel_src);
94 while(my($k,$v) = each %$other)
96 $self->check_has_relation($k, $v, $rel_src, $static);
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);
108 =method_public check_column_relation
110 Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column.
114 sub check_column_relation
116 my ($self, $col_rel, $static) = @_;
118 if(HashRef->check($col_rel))
122 while(my($k,$v) = each %$col_rel)
124 $self->check_has_relation($k, $v, undef, $static);
129 # not a relation but a column with a predicate
130 while(my($k, undef) = each %$col_rel)
132 $self->check_has_column($k);
138 $self->check_has_column($col_rel);