[Catalyst-commits] r7902 - in
Catalyst-Controller-DBIC-API/1.000/trunk: .
lib/Catalyst/Controller/DBIC lib/Catalyst/Controller/DBIC/API
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Mon Jun 9 19:17:11 BST 2008
Author: lukes
Date: 2008-06-09 19:17:10 +0100 (Mon, 09 Jun 2008)
New Revision: 7902
Removed:
Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/CRUD.pm
Modified:
Catalyst-Controller-DBIC-API/1.000/trunk/Makefile.PL
Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Log:
changes from zby: merged all base classes into one
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/Makefile.PL
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/Makefile.PL 2008-06-08 07:53:45 UTC (rev 7901)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/Makefile.PL 2008-06-09 18:17:10 UTC (rev 7902)
@@ -2,7 +2,7 @@
name 'Catalyst-Controller-DBIC-API';
perl_version '5.006001';
-all_from 'lib/Catalyst/Controller/DBIC/API.pm';
+all_from 'lib/Catalyst/Controller/DBIC/API/Base.pm';
requires 'DBIx::Class' => 0.08;
requires 'Catalyst' => 5.7000;
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-06-08 07:53:45 UTC (rev 7901)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-06-09 18:17:10 UTC (rev 7902)
@@ -2,8 +2,21 @@
use strict;
use warnings;
-use base 'Catalyst::Controller::DBIC::CRUD';
+use base 'Catalyst::Controller';
+__PACKAGE__->mk_accessors(qw(
+ class create_requires update_requires update_allows class_rs create_allows
+));
+
+__PACKAGE__->config(
+ class => undef,
+ create_requires => [],
+ create_allows => [],
+ update_requires => [],
+ update_allows => [],
+);
+
+
sub end :Private {
my ($self, $c) = @_;
@@ -38,6 +51,224 @@
$c->stash->{class_rs} = $c->model($self->class);
}
+
+
+=head2 create
+
+Matches cols specified in create_requires and create_allows to $c->req->params and $c->stash->{params}
+to create a hash representing a new object which is then created.
+
+=cut
+
+sub create :Private {
+ my ($self, $c) = @_;
+
+ unless (ref($self->create_requires) eq 'ARRAY') {
+ die "create_requires must be an arrayref in config";
+ }
+ unless ($c->stash->{class_rs}) {
+ die "class resultset not set";
+ }
+
+ my $empty_object = $c->stash->{class_rs}->new_result({});
+ $self->validate_and_save_object($c, $empty_object);
+}
+
+=head2 update
+
+Matches cols specified in update_allows to $c->req->params and $c->stash->{params}
+to create a hash which is used to update $c->stash->{object}
+
+=cut
+
+sub update :Private {
+ my ($self, $c) = @_;
+
+ return unless ($c->stash->{object});
+
+ unless (ref($self->update_allows) eq 'ARRAY') {
+ die "update_allows must be an arrayref in config";
+ }
+ unless ($c->stash->{class_rs}) {
+ die "class resultset not set";
+ }
+
+# use Data::Dumper; $c->log->debug(Dumper(\%create_args));
+ my $object = $c->stash->{object};
+ $self->validate_and_save_object($c, $object);
+}
+
+=head2 delete
+
+ Deletes $c->stash->{object}
+
+=cut
+
+sub delete :Private {
+ my ($self, $c) = @_;
+
+ return unless ($c->stash->{object});
+
+ return $c->stash->{object}->delete;
+}
+
+=head2 validate_and_save_object
+
+=cut
+
+sub validate_and_save_object {
+ my ($self, $c, $object) = @_;
+
+ my $params;
+ unless ($params = $self->validate($c, $object)) {
+ return;
+ }
+
+ $self->save_object($c, $object, $params);
+}
+
+=head2 validate
+
+=cut
+
+sub validate {
+ my ($self, $c, $object) = @_;
+ my $params = $c->req->params;
+
+ # operation specific hooks. pointless?
+# if ($object->in_storage) {
+# $self->validate_update($object, $params);
+# } else {
+# $self->validate_create($object, $params);
+# }
+
+ my %values;
+ my %requires_map = map { $_ => 1 } @{($object->in_storage) ? [] : $self->create_requires};
+ my %allows_map = map { $_ => 1 } (keys %requires_map, @{($object->in_storage) ? $self->update_allows : $self->create_allows});
+
+# use Data::Dumper; warn Dumper(\%requires_map, \%allows_map);
+ foreach my $key (keys %allows_map) {
+ # check value defined if key required
+ my $value = $params->{$key};
+ if ($requires_map{$key}) {
+ unless (defined($value)) {
+ # if not defined look for default
+ $value = $object->result_source
+ ->column_info($key)
+ ->{default_value};
+ unless (defined $value) {
+ $self->push_error($c, { message => "No value supplied for ${key} and no default" });
+ }
+ }
+ }
+
+ # TODO: do col type checking here
+
+ # check for multiple values
+ if (ref($value)) {
+ $self->push_error($c, { message => "Multiple values for '${key}'" });
+ }
+
+ # check exists so we don't just end up with hash of undefs
+ # check defined to accound for default values being used
+ $values{$key} = $value if exists $params->{$key} || defined $value;
+ }
+
+# use Data::Dumper; $c->log->debug(Dumper(\%values));
+ unless (keys %values || !$object->in_storage) {
+ $self->push_error($c, { message => "No valid keys passed" });
+ }
+
+# $c->stash->{_save_values}->{$object} = \%values;
+ return ($self->get_errors($c)) ? 0 : \%values;
+}
+
+sub validate_create { }
+sub validate_update { }
+
+=head2 save_object
+
+=cut
+
+sub save_object {
+ my ($self, $c, $object, $params) = @_;
+
+ $object->set_columns($params);
+ if ($object->in_storage) {
+ $object->update;
+ } else {
+ $object->insert;
+ }
+}
+
+# =head2 add_to_rel
+
+# finds a related row and then creates the many_to_many linking row using ->add_to_$rel
+# This is a work in progress, it should really allow the related row to also be created
+# if it does not already exist.
+
+# =cut
+
+# sub add_to_rel : Chained('object') PathPart('add_to_rel') Args(1) {
+# my ($self, $c, $rel) = @_;
+
+# my $accessor = "add_to_$rel";
+# $self->__rel($c, $rel, $accessor);
+# }
+
+# =head2 remove_from_rel
+
+# finds a related row and then removes the many_to_many linking row using ->remove_from_$rel
+
+# =cut
+
+# sub remove_from_rel : Chained('object') PathPart('remove_from_rel') Args(1) {
+# my ($self, $c, $rel) = @_;
+
+# my $accessor = "remove_from_$rel";
+# $self->__rel($c, $rel, $accessor);
+# }
+
+# sub __rel {
+# my ($self, $c, $rel, $accessor) = @_;
+
+# my $related_rs;
+# # pretty grim but i want it to work for many_to_many as well (so not $source->has_relationship)
+# eval {
+# $related_rs = $c->stash->{object}->$rel;
+# };
+# if ($@) {
+# $c->detach( 'error', [{ message => "Invalid relationship $rel" }]);
+# }
+# my $source = $related_rs->result_source;
+
+# my %related_args = map { $_ => $c->req->params->{$_} } grep { $c->req->params->{$_} } $source->columns;
+
+# unless (keys %related_args) {
+# $c->detach( 'error', [{ message => "No valid keys passed" }]);
+# }
+
+# my $related_row = $related_rs->result_source->resultset->find(\%related_args);
+# unless ($related_row) {
+# $c->detach( 'error', [{ message => "Invalid related row" }]);
+# }
+
+# $c->stash->{object}->$accessor($related_row);
+# }
+
+sub push_error {
+ my ( $self, $c, $params ) = @_;
+
+ push( @{$c->stash->{_dbic_crud_errors}}, $params->{message} || 'unknown error' );
+}
+
+sub get_errors {
+ my ( $self, $c, $params ) = @_;
+
+ return $c->stash->{_dbic_crud_errors};
+}
+
+
=head1 AUTHOR
luke saunders
Deleted: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/CRUD.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/CRUD.pm 2008-06-08 07:53:45 UTC (rev 7901)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/CRUD.pm 2008-06-09 18:17:10 UTC (rev 7902)
@@ -1,240 +0,0 @@
-package Catalyst::Controller::DBIC::CRUD;
-
-use strict;
-use warnings;
-use base 'Catalyst::Controller';
-
-__PACKAGE__->mk_accessors(qw(
- class create_requires update_requires update_allows class_rs create_allows
-));
-
-__PACKAGE__->config(
- class => undef,
- create_requires => [],
- create_allows => [],
- update_requires => [],
- update_allows => [],
-);
-
-=head2 create
-
-Matches cols specified in create_requires and create_allows to $c->req->params and $c->stash->{params}
-to create a hash representing a new object which is then created.
-
-=cut
-
-sub create :Private {
- my ($self, $c) = @_;
-
- unless (ref($self->create_requires) eq 'ARRAY') {
- die "create_requires must be an arrayref in config";
- }
- unless ($c->stash->{class_rs}) {
- die "class resultset not set";
- }
-
- my $empty_object = $c->stash->{class_rs}->new_result({});
- $self->validate_and_save_object($c, $empty_object);
-}
-
-=head2 update
-
-Matches cols specified in update_allows to $c->req->params and $c->stash->{params}
-to create a hash which is used to update $c->stash->{object}
-
-=cut
-
-sub update :Private {
- my ($self, $c) = @_;
-
- return unless ($c->stash->{object});
-
- unless (ref($self->update_allows) eq 'ARRAY') {
- die "update_allows must be an arrayref in config";
- }
- unless ($c->stash->{class_rs}) {
- die "class resultset not set";
- }
-
-# use Data::Dumper; $c->log->debug(Dumper(\%create_args));
- my $object = $c->stash->{object};
- $self->validate_and_save_object($c, $object);
-}
-
-=head2 delete
-
- Deletes $c->stash->{object}
-
-=cut
-
-sub delete :Private {
- my ($self, $c) = @_;
-
- return unless ($c->stash->{object});
-
- return $c->stash->{object}->delete;
-}
-
-=head2 validate_and_save_object
-
-=cut
-
-sub validate_and_save_object {
- my ($self, $c, $object) = @_;
-
- my $params;
- unless ($params = $self->validate($c, $object)) {
- return;
- }
-
- $self->save_object($c, $object, $params);
-}
-
-=head2 validate
-
-=cut
-
-sub validate {
- my ($self, $c, $object) = @_;
- my $params = $c->req->params;
-
- # operation specific hooks. pointless?
-# if ($object->in_storage) {
-# $self->validate_update($object, $params);
-# } else {
-# $self->validate_create($object, $params);
-# }
-
- my %values;
- my %requires_map = map { $_ => 1 } @{($object->in_storage) ? [] : $self->create_requires};
- my %allows_map = map { $_ => 1 } (keys %requires_map, @{($object->in_storage) ? $self->update_allows : $self->create_allows});
-
-# use Data::Dumper; warn Dumper(\%requires_map, \%allows_map);
- foreach my $key (keys %allows_map) {
- # check value defined if key required
- my $value = $params->{$key};
- if ($requires_map{$key}) {
- unless (defined($value)) {
- # if not defined look for default
- $value = $object->result_source
- ->column_info($key)
- ->{default_value};
- unless (defined $value) {
- $self->push_error($c, { message => "No value supplied for ${key} and no default" });
- }
- }
- }
-
- # TODO: do col type checking here
-
- # check for multiple values
- if (ref($value)) {
- $self->push_error($c, { message => "Multiple values for '${key}'" });
- }
-
- # check exists so we don't just end up with hash of undefs
- # check defined to accound for default values being used
- $values{$key} = $value if exists $params->{$key} || defined $value;
- }
-
-# use Data::Dumper; $c->log->debug(Dumper(\%values));
- unless (keys %values || !$object->in_storage) {
- $self->push_error($c, { message => "No valid keys passed" });
- }
-
-# $c->stash->{_save_values}->{$object} = \%values;
- return ($self->get_errors($c)) ? 0 : \%values;
-}
-
-sub validate_create { }
-sub validate_update { }
-
-=head2 save_object
-
-=cut
-
-sub save_object {
- my ($self, $c, $object, $params) = @_;
-
- $object->set_columns($params);
- if ($object->in_storage) {
- $object->update;
- } else {
- $object->insert;
- }
-}
-
-# =head2 add_to_rel
-
-# finds a related row and then creates the many_to_many linking row using ->add_to_$rel
-# This is a work in progress, it should really allow the related row to also be created
-# if it does not already exist.
-
-# =cut
-
-# sub add_to_rel : Chained('object') PathPart('add_to_rel') Args(1) {
-# my ($self, $c, $rel) = @_;
-
-# my $accessor = "add_to_$rel";
-# $self->__rel($c, $rel, $accessor);
-# }
-
-# =head2 remove_from_rel
-
-# finds a related row and then removes the many_to_many linking row using ->remove_from_$rel
-
-# =cut
-
-# sub remove_from_rel : Chained('object') PathPart('remove_from_rel') Args(1) {
-# my ($self, $c, $rel) = @_;
-
-# my $accessor = "remove_from_$rel";
-# $self->__rel($c, $rel, $accessor);
-# }
-
-# sub __rel {
-# my ($self, $c, $rel, $accessor) = @_;
-
-# my $related_rs;
-# # pretty grim but i want it to work for many_to_many as well (so not $source->has_relationship)
-# eval {
-# $related_rs = $c->stash->{object}->$rel;
-# };
-# if ($@) {
-# $c->detach( 'error', [{ message => "Invalid relationship $rel" }]);
-# }
-# my $source = $related_rs->result_source;
-
-# my %related_args = map { $_ => $c->req->params->{$_} } grep { $c->req->params->{$_} } $source->columns;
-
-# unless (keys %related_args) {
-# $c->detach( 'error', [{ message => "No valid keys passed" }]);
-# }
-
-# my $related_row = $related_rs->result_source->resultset->find(\%related_args);
-# unless ($related_row) {
-# $c->detach( 'error', [{ message => "Invalid related row" }]);
-# }
-
-# $c->stash->{object}->$accessor($related_row);
-# }
-
-sub push_error {
- my ( $self, $c, $params ) = @_;
-
- push( @{$c->stash->{_dbic_crud_errors}}, $params->{message} || 'unknown error' );
-}
-
-sub get_errors {
- my ( $self, $c, $params ) = @_;
-
- return $c->stash->{_dbic_crud_errors};
-}
-
-=head1 AUTHOR
-
-luke saunders
-
-=cut
-
-1;
More information about the Catalyst-commits
mailing list