X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FSchema%2FIndex.pm;h=99eb38ce3353528ff2a5a3b1299824a9c38e99ac;hb=2bdef63659f546187a4d1266e88aa66671b228b7;hp=7834350a50f0c29fa4e1329f6ea9570c5306da07;hpb=2cb3ea5562c93d010f7000605e0764719d29ddd3;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index 7834350..99eb38c 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -25,18 +25,14 @@ Primary and unique keys are table constraints, not indices. =cut -use Moo; +use Moo 1.000003; use SQL::Translator::Schema::Constants; -use SQL::Translator::Utils 'parse_list_arg'; -use List::MoreUtils qw(uniq); +use SQL::Translator::Utils qw(ex2err throw); +use SQL::Translator::Role::ListAttr; +use SQL::Translator::Types qw(schema_obj); +use Sub::Quote qw(quote_sub); -with qw( - SQL::Translator::Schema::Role::Extra - SQL::Translator::Schema::Role::Error - SQL::Translator::Schema::Role::Compare -); - -our ( $TABLE_COUNT, $VIEW_COUNT ); +extends 'SQL::Translator::Schema::Object'; our $VERSION = '1.59'; @@ -54,14 +50,6 @@ Object constructor. my $schema = SQL::Translator::Schema::Index->new; -=cut - -sub BUILD { - my ($self) = @_; - $self->$_(scalar $self->$_) - foreach qw(fields options); -} - =head2 fields Gets and set the fields the index is on. Accepts a string, list or @@ -78,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 { @@ -126,7 +101,7 @@ Get or set the index's name. =cut -has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } ); +has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => quote_sub(q{ '' }) ); =head2 options @@ -137,21 +112,7 @@ an array or array reference. =cut -has options => ( - is => 'rw', - default => sub { [] }, - coerce => sub { parse_list_arg($_[0]) }, -); - -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 @@ -161,18 +122,9 @@ Get or set the index's table object. =cut -has table => ( is => 'rw' ); - -around table => sub { - my $orig = shift; - my $self = shift; - if ( my $arg = $_[0] ) { - return $self->error('Not a table object') unless - UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' ); - } +has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 ); - return $self->$orig(@_); -}; +around table => \&ex2err; =head2 type @@ -189,19 +141,17 @@ uppercase. =cut -has type => ( is => 'rw', default => sub { 'NORMAL' } ); - -around type => sub { - my ( $orig, $self) = (shift, shift); +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 => quote_sub(q{ 'NORMAL' }), +); - if ( my $type = $_[0] ) { - my $type = uc $type; - return $self->error("Invalid index type: $type") - unless $VALID_INDEX_TYPE{ $type }; - } - - return $self->$orig(@_); -}; +around type => \&ex2err; =head2 equals @@ -248,10 +198,8 @@ 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; 1;