From 931cbc8d6673ec352b369ae2f70f01ff96b4f507 Mon Sep 17 00:00:00 2001 From: groditi Date: Fri, 11 Sep 2009 13:36:16 +0000 Subject: initial refactor of CRUD controller actions as roles --- lib/Reaction/UI/Controller/Role/GetCollection.pm | 104 +++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 lib/Reaction/UI/Controller/Role/GetCollection.pm (limited to 'lib/Reaction/UI/Controller/Role/GetCollection.pm') diff --git a/lib/Reaction/UI/Controller/Role/GetCollection.pm b/lib/Reaction/UI/Controller/Role/GetCollection.pm new file mode 100644 index 0000000..8f40ce2 --- /dev/null +++ b/lib/Reaction/UI/Controller/Role/GetCollection.pm @@ -0,0 +1,104 @@ +package Reaction::UI::Controller::Role::GetCollection; + +use Moose::Role -traits => 'MethodAttributes'; + +has model_name => (isa => 'Str', is => 'rw', required => 1); +has collection_name => (isa => 'Str', is => 'rw', required => 1); + +sub get_collection { + my ($self, $c) = @_; + my $model = $c->model( $self->model_name ); + confess "Failed to find Catalyst model named: " . $self->model_name + unless $model; + my $collection = $self->collection_name; + if( my $meth = $model->can( $collection ) ){ + return $model->$meth; + } elsif ( my $meta = $model->can('meta') ){ + if ( my $attr = $model->$meta->find_attribute_by_name($collection) ) { + my $reader = $attr->get_read_method; + return $model->$reader; + } + } + confess "Failed to find collection $collection"; +} + +1; + +__END__; + +=head1 NAME + +Reaction::UI::Controller::Role::GetCollection + +=head1 DESCRIPTION + +Provides a C method, which fetches an C object +from a specified model. + +=head1 SYNOPSYS + + package MyApp::Controller::Foo; + + use base 'Reaction::Controller'; + use Reaction::Class; + + with 'Reaction::UI::Controller::Role::GetCollection'; + + __PACKAGE__->config( model_name => 'MyAppIM', collection_name => 'foos' ); + + sub bar :Local { + my($self, $c) = @_; + my $obj = $self->get_collection($c)->find( $some_key ); + } + +=head1 ATTRIBUTES + +=head2 model_name + +The name of the model this controller will use as it's data source. Should be a +name that can be passed to C<$C-Emodel> + +=head2 collection_name + +The name of the collection whithin the model that this Controller will be +utilizing. + +=head1 METHODS + +=head2 get_collection $c + +Returns an instance of the collection this controller uses. + +=head1 SEE ALSO + +=over4 + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=back + +=head1 AUTHORS + +See L for authors. + +=head1 LICENSE + +See L for the license. + +=cut -- cgit v1.2.3-54-g00ecf