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 => Maybe[Str], default => 'DBIx::Class::ResultClass::HashRefInflator' );
28 =method_public stored_result_source
30 This is the result source for the controller
34 sub stored_result_source
36 return shift->stored_model->result_source;
39 =method_public stored_model
41 This is the model for the controller
47 return $_[0]->_application->model($_[0]->class);
50 =method_public check_has_column
52 Convenience method for checking if the column exists in the result source
58 my ($self, $col) = @_;
59 die "Column '$col' does not exist in ResultSet '${\$self->class}'"
60 unless $self->stored_result_source->has_column($col);
63 =method_public check_has_relation
65 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.
69 sub check_has_relation
71 my ($self, $rel, $other, $nest, $static) = @_;
73 $nest ||= $self->stored_result_source;
75 if(HashRef->check($other))
77 my $rel_src = $nest->related_source($rel);
78 die "Relation '$rel_src' does not exist" if not defined($rel_src);
80 while(my($k,$v) = each %$other)
82 $self->check_has_relation($k, $v, $rel_src, $static);
87 return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*';
88 die "Relation '$rel' does not exist in ${\$nest->from}"
89 unless $nest->has_relationship($rel) || $nest->has_column($rel);
94 =method_public check_column_relation
96 Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column.
100 sub check_column_relation
102 my ($self, $col_rel, $static) = @_;
104 if(HashRef->check($col_rel))
108 while(my($k,$v) = each %$col_rel)
110 $self->check_has_relation($k, $v, undef, $static);
115 # not a relation but a column with a predicate
116 while(my($k, undef) = each %$col_rel)
118 $self->check_has_column($k);
124 $self->check_has_column($col_rel);