Use quote_sub for trivial defaults
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
index 6e2861e..c19501b 100644 (file)
@@ -24,16 +24,12 @@ C<SQL::Translator::Schema::View> is the view object.
 =cut
 
 use Moo;
-use SQL::Translator::Utils qw(parse_list_arg ex2err);
+use SQL::Translator::Utils qw(ex2err);
 use SQL::Translator::Types qw(schema_obj);
-use List::MoreUtils qw(uniq);
+use SQL::Translator::Role::ListAttr;
+use Sub::Quote qw(quote_sub);
 
-with qw(
-  SQL::Translator::Schema::Role::BuildArgs
-  SQL::Translator::Schema::Role::Extra
-  SQL::Translator::Schema::Role::Error
-  SQL::Translator::Schema::Role::Compare
-);
+extends 'SQL::Translator::Schema::Object';
 
 our $VERSION = '1.59';
 
@@ -59,20 +55,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 );
 
 =head2 tables
 
@@ -90,20 +73,7 @@ names and keep them in order by the first occurrence of a field name.
 
 =cut
 
-has tables => (
-    is => 'rw',
-    default => sub { [] },
-    coerce => sub { [uniq @{parse_list_arg($_[0])}] },
-);
-
-around tables => 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 tables => ( uniq => 1 );
 
 =head2 options
 
@@ -115,23 +85,7 @@ Gets and sets a list of options on the view.
 
 =cut
 
-has options => (
-    is => 'rw',
-    default => sub { [] },
-    coerce => sub { [uniq @{parse_list_arg($_[0])}] },
-);
-
-around options => sub {
-    my $orig    = shift;
-    my $self    = shift;
-    my $options = parse_list_arg( @_ );
-
-    if ( @$options ) {
-        $self->$orig([ @{$self->$orig}, @$options ])
-    }
-
-    return wantarray ? @{ $self->$orig } : $self->$orig;
-};
+with ListAttr options => ( uniq => 1, append => 1 );
 
 sub is_valid {
 
@@ -161,7 +115,7 @@ Get or set the view's name.
 
 =cut
 
-has name => ( is => 'rw', default => sub { '' } );
+has name => ( is => 'rw', default => quote_sub(q{ '' }) );
 
 =head2 order
 
@@ -171,7 +125,7 @@ Get or set the view'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 ) = @_;
@@ -191,7 +145,7 @@ Get or set the view's SQL.
 
 =cut
 
-has sql => ( is => 'rw', default => sub { '' } );
+has sql => ( is => 'rw', default => quote_sub(q{ '' }) );
 
 =head2 schema
 
@@ -202,7 +156,7 @@ Get or set the view'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;
 
@@ -244,11 +198,6 @@ around equals => sub {
     return 1;
 };
 
-sub DESTROY {
-    my $self = shift;
-    undef $self->{'schema'}; # destroy cyclical reference
-}
-
 # Must come after all 'has' declarations
 around new => \&ex2err;