'File::Spec' => '0',
'XML::Writer' => '0.500',
'Moo' => '1.000003',
+ 'Package::Variant' => '1.001001',
'Try::Tiny' => '0.04',
},
recommends => {
--- /dev/null
+package SQL::Translator::Role::ListAttr;
+use strictures 1;
+use SQL::Translator::Utils qw(parse_list_arg ex2err);
+use List::MoreUtils qw(uniq);
+
+use Package::Variant (
+ importing => {
+ 'Moo::Role' => [],
+ },
+ subs => [qw(has around)],
+);
+
+
+sub make_variant {
+ my ($class, $target_package, $name, %arguments) = @_;
+
+ my $may_throw = delete $arguments{may_throw};
+ my $undef_if_empty = delete $arguments{undef_if_empty};
+ my $append = delete $arguments{append};
+ my $coerce = delete $arguments{uniq}
+ ? sub { [ uniq @{parse_list_arg($_[0])} ] }
+ : \&parse_list_arg;
+
+ has($name => (
+ is => 'rw',
+ (!$arguments{builder} ? (
+ default => sub { [] },
+ ) : ()),
+ coerce => $coerce,
+ %arguments,
+ ));
+
+ around($name => sub {
+ my ($orig, $self) = (shift, shift);
+ my $list = parse_list_arg(@_);
+ $self->$orig([ @{$append ? $self->$orig : []}, @$list ])
+ if @$list;
+
+ my $return;
+ if ($may_throw) {
+ $return = ex2err($orig, $self) or return;
+ }
+ else {
+ $return = $self->$orig;
+ }
+ my $scalar_return = !@{$return} && $undef_if_empty ? undef : $return;
+ return wantarray ? @{$return} : $scalar_return;
+ });
+}
+
+1;
use Moo;
use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
+use SQL::Translator::Utils qw(ex2err throw);
+use SQL::Translator::Role::ListAttr;
use SQL::Translator::Types qw(schema_obj);
-use List::MoreUtils qw(uniq);
extends 'SQL::Translator::Schema::Object';
=cut
-has field_names => (
- is => 'rw',
- default => sub { [] },
- coerce => sub { [uniq @{parse_list_arg($_[0])}] },
-);
-
-around field_names => sub {
- my $orig = shift;
- my $self = shift;
- my $fields = parse_list_arg( @_ );
- $self->$orig($fields) if @$fields;
-
- $fields = $self->$orig;
- return wantarray ? @{$fields}
- : @{$fields} ? $fields
- : undef;
-};
+with ListAttr field_names => ( uniq => 1, undef_if_empty => 1 );
=head2 match_type
=cut
-has options => ( is => 'rw', coerce => \&parse_list_arg, default => sub { [] } );
-
-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 on_delete
=cut
-has reference_fields => (
- is => 'rw',
- coerce => sub { [uniq @{parse_list_arg($_[0])}] },
+with ListAttr reference_fields => (
+ may_throw => 1,
builder => 1,
lazy => 1,
);
-around reference_fields => sub {
- my $orig = shift;
- my $self = shift;
- my $fields = parse_list_arg( @_ );
- $self->$orig($fields) if @$fields;
-
- $fields = ex2err($orig, $self) or return;
- return wantarray ? @{$fields} : $fields
-};
-
sub _build_reference_fields {
my ($self) = @_;
use Moo;
use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
+use SQL::Translator::Utils qw(ex2err throw);
+use SQL::Translator::Role::ListAttr;
use SQL::Translator::Types qw(schema_obj);
-use List::MoreUtils qw(uniq);
extends 'SQL::Translator::Schema::Object';
=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 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
=cut
use Moo;
-use SQL::Translator::Utils qw(parse_list_arg ex2err);
+use SQL::Translator::Utils qw(ex2err);
+use SQL::Translator::Role::ListAttr;
use SQL::Translator::Types qw(schema_obj);
-use List::MoreUtils qw(uniq);
extends 'SQL::Translator::Schema::Object';
=cut
-has parameters => (
- is => 'rw',
- default => sub { [] },
- coerce => sub { [uniq @{parse_list_arg($_[0])}] },
-);
-
-around parameters => 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 parameters => ( uniq => 1 );
=head2 name
use Moo;
use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
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;
=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
=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;
extends 'SQL::Translator::Schema::Object';
=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
=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
=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 {