X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FSchema%2FIndex.pm;h=4d5ac2dee762ce36750b1014b737915e2430aeaf;hb=f769b7e83b290b2500a1860d35527ff41df64d99;hp=0d8a016dbe57d89f6294c1f23f0cce4387a74044;hpb=f8faea1df6a14defdcb4edc23fea18567f13c12b;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index 0d8a016..4d5ac2d 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -27,18 +27,14 @@ Primary and unique keys are table constraints, not indices. use Moo; use SQL::Translator::Schema::Constants; -use SQL::Translator::Utils qw(parse_list_arg ex2err throw); -use SQL::Translator::Types qw(schema_obj); -use List::MoreUtils qw(uniq); - -with qw( - SQL::Translator::Schema::Role::BuildArgs - SQL::Translator::Schema::Role::Extra - SQL::Translator::Schema::Role::Error - SQL::Translator::Schema::Role::Compare -); +use SQL::Translator::Utils qw(ex2err throw); +use SQL::Translator::Role::ListAttr; +use SQL::Translator::Types qw(schema_obj enum); +use Sub::Quote qw(quote_sub); + +extends 'SQL::Translator::Schema::Object'; -our $VERSION = '1.59'; +our $VERSION = '1.62'; my %VALID_INDEX_TYPE = ( UNIQUE => 1, @@ -70,20 +66,7 @@ names and keep them in order by the first occurrence of a field name. =cut -has fields => ( - is => 'rw', - default => sub { [] }, - coerce => sub { [uniq @{parse_list_arg($_[0])}] }, -); - -around fields => sub { - my $orig = shift; - my $self = shift; - my $fields = parse_list_arg( @_ ); - $self->$orig($fields) if @$fields; - - return wantarray ? @{ $self->$orig } : $self->$orig; -}; +with ListAttr fields => ( uniq => 1 ); sub is_valid { @@ -118,7 +101,11 @@ Get or set the index's name. =cut -has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } ); +has name => ( + is => 'rw', + coerce => quote_sub(q{ defined $_[0] ? $_[0] : '' }), + default => quote_sub(q{ '' }), +); =head2 options @@ -129,21 +116,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 => (); =head2 table @@ -153,7 +126,7 @@ Get or set the index's table object. =cut -has table => ( is => 'rw', isa => schema_obj('Table') ); +has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 ); around table => \&ex2err; @@ -174,12 +147,11 @@ uppercase. has type => ( is => 'rw', - isa => sub { - my $type = uc $_[0] or return; - throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type}; - }, - coerce => sub { uc $_[0] }, - default => sub { 'NORMAL' }, + coerce => quote_sub(q{ uc $_[0] }), + default => quote_sub(q{ 'NORMAL' }), + isa => enum([keys %VALID_INDEX_TYPE], { + msg => "Invalid index type: %s", allow_false => 1, + }), ); around type => \&ex2err; @@ -229,11 +201,6 @@ around equals => sub { return 1; }; -sub DESTROY { - my $self = shift; - undef $self->{'table'}; # destroy cyclical reference -} - # Must come after all 'has' declarations around new => \&ex2err;