X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FDBIC%2FAPI%2FStoredResultSource.pm;h=763165488a164943a53ec0f1696eee537a17c378;hb=HEAD;hp=dc8859b55178fa0dff4e5bc6959d18500c62e3e5;hpb=d72e59272b503e4e32c61975658fc356391f379f;p=catagits%2FCatalyst-Controller-DBIC-API.git diff --git a/lib/Catalyst/Controller/DBIC/API/StoredResultSource.pm b/lib/Catalyst/Controller/DBIC/API/StoredResultSource.pm index dc8859b..7631654 100644 --- a/lib/Catalyst/Controller/DBIC/API/StoredResultSource.pm +++ b/lib/Catalyst/Controller/DBIC/API/StoredResultSource.pm @@ -1,4 +1,5 @@ package Catalyst::Controller::DBIC::API::StoredResultSource; + #ABSTRACT: Provides accessors for static resources use Moose::Role; @@ -9,48 +10,48 @@ use namespace::autoclean; requires '_application'; -=attribute_public class is: ro, isa: Str +=attribute_public class -class is the name of the class that is the model for this controller +The name of the Catalyst model for this controller. =cut has 'class' => ( is => 'ro', isa => Str, writer => '_set_class' ); -=attribute_public stored_result_source is: ro, isa: L +=attribute_public result_class -This is the result source for the controller +Populates the result_class attribute of resultsets. =cut -has 'stored_result_source' => -( - is => 'ro', - isa => ResultSource, - lazy_build => 1, +has 'result_class' => ( + is => 'ro', + isa => Maybe [Str], + default => 'DBIx::Class::ResultClass::HashRefInflator' ); -=attribute_public stored_model is: ro, isa: L +=method_public stored_result_source -This is the model for the controller +Returns the result_source of the stored_model. =cut -has 'stored_model' => -( - is => 'ro', - isa => Model, - lazy_build => 1, -); - -sub _build_stored_model -{ - return $_[0]->_application->model($_[0]->class); +sub stored_result_source { + return shift->stored_model->result_source; } -sub _build_stored_result_source -{ - return shift->stored_model->result_source(); +=method_public stored_model + +Returns the model for the configured class. + +Be aware that model is called as a class method on the Catalyst application +and not as an instance method on $c which might lead to unexpected results +in conjunction with ACCEPT_CONTEXT! + +=cut + +sub stored_model { + return $_[0]->_application->model( $_[0]->class ); } =method_public check_has_column @@ -59,37 +60,36 @@ Convenience method for checking if the column exists in the result source =cut -sub check_has_column -{ - my ($self, $col) = @_; +sub check_has_column { + my ( $self, $col ) = @_; die "Column '$col' does not exist in ResultSet '${\$self->class}'" unless $self->stored_result_source->has_column($col); } =method_public check_has_relation -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. +check_has_relation meticulously delves into the result sources relationships to +determine if the provided relation is valid. +Accepts a relation name, an 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. =cut -sub check_has_relation -{ - my ($self, $rel, $other, $nest, $static) = @_; +sub check_has_relation { + my ( $self, $rel, $other, $nest, $static ) = @_; $nest ||= $self->stored_result_source; - if(HashRef->check($other)) - { + if ( HashRef->check($other) ) { my $rel_src = $nest->related_source($rel); die "Relation '$rel_src' does not exist" if not defined($rel_src); - while(my($k,$v) = each %$other) - { - $self->check_has_relation($k, $v, $rel_src, $static); + while ( my ( $k, $v ) = each %$other ) { + $self->check_has_relation( $k, $v, $rel_src, $static ); } } - else - { + else { return 1 if $static && ArrayRef->check($other) && $other->[0] eq '*'; die "Relation '$rel' does not exist in ${\$nest->from}" unless $nest->has_relationship($rel) || $nest->has_column($rel); @@ -99,34 +99,28 @@ sub check_has_relation =method_public check_column_relation -Convenience method to first check if the provided argument is a valid relation (if it is a HashRef) or column. +Convenience method to first check if the provided argument is a valid relation +(if it is a HashRef) or column. =cut -sub check_column_relation -{ - my ($self, $col_rel, $static) = @_; - - if(HashRef->check($col_rel)) - { - try - { - while(my($k,$v) = each %$col_rel) - { - $self->check_has_relation($k, $v, undef, $static); +sub check_column_relation { + my ( $self, $col_rel, $static ) = @_; + + if ( HashRef->check($col_rel) ) { + try { + while ( my ( $k, $v ) = each %$col_rel ) { + $self->check_has_relation( $k, $v, undef, $static ); } } - catch - { + catch { # not a relation but a column with a predicate - while(my($k, undef) = each %$col_rel) - { + while ( my ( $k, undef ) = each %$col_rel ) { $self->check_has_column($k); } } } - else - { + else { $self->check_has_column($col_rel); } }