=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';
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
=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 {
=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
=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
=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
=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
return 1;
};
-sub DESTROY {
- my $self = shift;
- undef $self->{'table'}; # destroy cyclical reference
-}
+# Must come after all 'has' declarations
+around new => \&ex2err;
1;