Factor list attributes into variant role
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
index a7ee464..38c7ffe 100644 (file)
@@ -22,6 +22,7 @@ C<SQL::Translator::Schema::Table> is the table object.
 use Moo;
 use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
 use SQL::Translator::Types qw(schema_obj);
+use SQL::Translator::Role::ListAttr;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Schema::Constraint;
 use SQL::Translator::Schema::Field;
@@ -30,11 +31,7 @@ use SQL::Translator::Schema::Index;
 use Carp::Clan '^SQL::Translator';
 use List::Util 'max';
 
-with qw(
-  SQL::Translator::Schema::Role::Extra
-  SQL::Translator::Schema::Role::Error
-  SQL::Translator::Schema::Role::Compare
-);
+extends 'SQL::Translator::Schema::Object';
 
 our $VERSION = '1.59';
 
@@ -334,7 +331,7 @@ sub add_field {
     my $field_name = $field->name;
 
     if ( $self->get_field($field_name) ) {
-        return $self->error(qq[Can't create field: "$field_name" exists]);
+        return $self->error(qq[Can't use field name "$field_name": field exists]);
     }
     else {
         $self->_fields->{ $field_name } = $field;
@@ -722,7 +719,7 @@ Get or set the table's schema object.
 
 =cut
 
-has schema => ( is => 'rw', isa => schema_obj('Schema') );
+has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
 
 around schema => \&ex2err;
 
@@ -800,21 +797,7 @@ an array or array reference.
 
 =cut
 
-has options => (
-    is => 'rw',
-    default => sub { [] },
-    coerce => \&parse_list_arg,
-);
-
-around options => sub {
-    my $orig    = shift;
-    my $self    = shift;
-    my $options = parse_list_arg( @_ );
-
-    push @{ $self->$orig }, @$options;
-
-    return wantarray ? @{ $self->$orig } : $self->$orig;
-};
+with ListAttr options => ( append => 1 );
 
 =head2 order
 
@@ -1035,14 +1018,6 @@ sub fkey_constraints {
     return wantarray ? @cons : \@cons;
 }
 
-sub DESTROY {
-    my $self = shift;
-    undef $self->{'schema'}; # destroy cyclical reference
-    undef $_ for @{ $self->{'constraints'} };
-    undef $_ for @{ $self->{'indices'} };
-    undef $_ for values %{ $self->{'fields'} };
-}
-
 # Must come after all 'has' declarations
 around new => \&ex2err;