Bumping version to 1.59_01
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
index a7ee464..17caff7 100644 (file)
@@ -20,8 +20,9 @@ C<SQL::Translator::Schema::Table> is the table object.
 =cut
 
 use Moo;
-use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
+use SQL::Translator::Utils qw(parse_list_arg ex2err throw carp_ro);
 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;
@@ -29,14 +30,11 @@ use SQL::Translator::Schema::Index;
 
 use Carp::Clan '^SQL::Translator';
 use List::Util 'max';
+use Sub::Quote qw(quote_sub);
 
-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';
+our $VERSION = '1.59_01';
 
 # Stringify to our name, being careful not to pass any args through so we don't
 # accidentally set it to undef. We also have to tweak bool so the object is
@@ -77,7 +75,7 @@ C<SQL::Translator::Schema::Constraint> object.
 has _constraints => (
     is => 'ro',
     init_arg => undef,
-    default => sub { +[] },
+    default => quote_sub(q{ +[] }),
     predicate => 1,
     lazy => 1,
 );
@@ -200,7 +198,7 @@ C<SQL::Translator::Schema::Index> object.
 has _indices => (
     is => 'ro',
     init_arg => undef,
-    default => sub { [] },
+    default => quote_sub(q{ [] }),
     predicate => 1,
     lazy => 1,
 );
@@ -284,7 +282,7 @@ existing field, you will get an error and the field will not be created.
 has _fields => (
     is => 'ro',
     init_arg => undef,
-    default => sub { +{} },
+    default => quote_sub(q{ +{} }),
     predicate => 1,
     lazy => 1
 );
@@ -334,7 +332,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;
@@ -407,8 +405,8 @@ all the comments joined on newlines.
 
 has comments => (
     is => 'rw',
-    coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] },
-    default => sub { [] },
+    coerce => quote_sub(q{ ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] }),
+    default => quote_sub(q{ [] }),
 );
 
 around comments => sub {
@@ -444,7 +442,7 @@ sub get_constraints {
     }
     else {
         $self->error('No constraints');
-        return wantarray ? () : undef;
+        return;
     }
 }
 
@@ -466,7 +464,7 @@ sub get_indices {
     }
     else {
         $self->error('No indices');
-        return wantarray ? () : undef;
+        return;
     }
 }
 
@@ -517,7 +515,7 @@ sub get_fields {
     }
     else {
         $self->error('No fields');
-        return wantarray ? () : undef;
+        return;
     }
 }
 
@@ -551,6 +549,8 @@ True if table has no data (non-key) fields and only uses single key joins.
 
 has is_trivial_link => ( is => 'lazy', init_arg => undef );
 
+around is_trivial_link => carp_ro('is_trivial_link');
+
 sub _build_is_trivial_link {
     my $self = shift;
     return 0 if $self->is_data;
@@ -579,6 +579,8 @@ Returns true if the table has some non-key fields.
 
 has is_data => ( is => 'lazy', init_arg => undef );
 
+around is_data => carp_ro('is_data');
+
 sub _build_is_data {
     my $self = shift;
 
@@ -599,7 +601,7 @@ Determine whether the table can link two arg tables via many-to-many.
 
 =cut
 
-has _can_link => ( is => 'ro', init_arg => undef, default => sub { +{} } );
+has _can_link => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) );
 
 sub can_link {
     my ( $self, $table1, $table2 ) = @_;
@@ -722,7 +724,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;
 
@@ -739,7 +741,7 @@ add to the fields of an existing PK (and will unique the field names).
 Returns the C<SQL::Translator::Schema::Constraint> object representing
 the primary key.
 
-These are eqivalent:
+These are equivalent:
 
   $table->primary_key('id');
   $table->primary_key(['name']);
@@ -793,28 +795,14 @@ These are eqivalent:
 
 =head2 options
 
-Get or set the table's options (e.g., table types for MySQL).  Returns
-an array or array reference.
+Get or append to the table's options (e.g., table types for MySQL).
+Returns an array or array reference.
 
   my @options = $table->options;
 
 =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
 
@@ -824,7 +812,7 @@ Get or set the table's order.
 
 =cut
 
-has order => ( is => 'rw', default => sub { 0 } );
+has order => ( is => 'rw', default => quote_sub(q{ 0 }) );
 
 around order => sub {
     my ( $orig, $self, $arg ) = @_;
@@ -857,7 +845,7 @@ sub field_names {
     }
     else {
         $self->error('No fields');
-        return wantarray ? () : undef;
+        return;
     }
 }
 
@@ -1035,14 +1023,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;