Cleanup ::ResultSourceProxy, use a proxy-method generator
Peter Rabbitson [Sun, 12 Sep 2010 18:50:34 +0000 (20:50 +0200)]
lib/DBIx/Class/ResultSourceProxy.pm

index 9975540..859b397 100644 (file)
@@ -6,31 +6,22 @@ use warnings;
 
 use base qw/DBIx::Class/;
 use Scalar::Util qw/blessed/;
-use Carp::Clan qw/^DBIx::Class/;
+use Sub::Name qw/subname/;
+use namespace::clean;
 
-sub iterator_class  { shift->result_source_instance->resultset_class(@_) }
-sub resultset_class { shift->result_source_instance->resultset_class(@_) }
-sub result_class { shift->result_source_instance->result_class(@_) }
-sub source_info { shift->result_source_instance->source_info(@_) }
+__PACKAGE__->mk_group_accessors('inherited_ro_instance' => 'source_name');
+
+sub get_inherited_ro_instance {  shift->get_inherited(@_) }
 
 sub set_inherited_ro_instance {
-    my $self = shift;
+  my $self = shift;
 
-    croak "Cannot set @{[shift]} on an instance" if blessed $self;
+  $self->throw_exception ("Cannot set @{[shift]} on an instance")
+    if blessed $self;
 
-    return $self->set_inherited(@_);
+  $self->set_inherited(@_);
 }
 
-sub get_inherited_ro_instance {
-    return shift->get_inherited(@_);
-}
-
-__PACKAGE__->mk_group_accessors('inherited_ro_instance' => 'source_name');
-
-
-sub resultset_attributes {
-  shift->result_source_instance->resultset_attributes(@_);
-}
 
 sub add_columns {
   my ($class, @cols) = @_;
@@ -44,80 +35,54 @@ sub add_columns {
   }
 }
 
-sub add_column {
-  shift->add_columns(@_);
-}
-
-sub has_column {
-  shift->result_source_instance->has_column(@_);
-}
-
-sub column_info {
-  shift->result_source_instance->column_info(@_);
-}
-
-sub column_info_from_storage {
-  shift->result_source_instance->column_info_from_storage(@_);
-}
+sub add_column { shift->add_columns(@_) }
 
-sub columns {
-  shift->result_source_instance->columns(@_);
-}
-
-sub remove_columns {
-  shift->result_source_instance->remove_columns(@_);
-}
 
-*remove_column = \&remove_columns;
-
-sub set_primary_key {
-  shift->result_source_instance->set_primary_key(@_);
+sub add_relationship {
+  my ($class, $rel, @rest) = @_;
+  my $source = $class->result_source_instance;
+  $source->add_relationship($rel => @rest);
+  $class->register_relationship($rel => $source->relationship_info($rel));
 }
 
-sub primary_columns {
-  shift->result_source_instance->primary_columns(@_);
-}
 
-sub _pri_cols {
-  shift->result_source_instance->_pri_cols(@_);
-}
+# legacy resultset_class accessor, seems to be used by cdbi only
+sub iterator_class { shift->result_source_instance->resultset_class(@_) }
 
-sub add_unique_constraint {
-  shift->result_source_instance->add_unique_constraint(@_);
-}
+for my $method_to_proxy (qw/
+  source_info
+  result_class
+  resultset_class
+  resultset_attributes
 
-sub add_unique_constraints {
-  shift->result_source_instance->add_unique_constraints(@_);
-}
+  columns
+  has_column
 
-sub unique_constraints {
-  shift->result_source_instance->unique_constraints(@_);
-}
+  remove_column
+  remove_columns
 
-sub unique_constraint_names {
-  shift->result_source_instance->unique_constraint_names(@_);
-}
+  column_info
+  column_info_from_storage
 
-sub unique_constraint_columns {
-  shift->result_source_instance->unique_constraint_columns(@_);
-}
+  set_primary_key
+  primary_columns
+  _pri_cols
 
-sub add_relationship {
-  my ($class, $rel, @rest) = @_;
-  my $source = $class->result_source_instance;
-  $source->add_relationship($rel => @rest);
-  $class->register_relationship($rel => $source->relationship_info($rel));
-}
+  add_unique_constraint
+  add_unique_constraints
 
-sub relationships {
-  shift->result_source_instance->relationships(@_);
-}
+  unique_constraints
+  unique_constraint_names
+  unique_constraint_columns
 
-sub relationship_info {
-  shift->result_source_instance->relationship_info(@_);
+  relationships
+  relationship_info
+  has_relationship
+/) {
+  no strict qw/refs/;
+  *{__PACKAGE__."::$method_to_proxy"} = subname $method_to_proxy => sub {
+    shift->result_source_instance->$method_to_proxy (@_);
+  };
 }
 
-sub has_relationship {
-  shift->result_source_instance->has_relationship(@_);
-}
 1;