Skip to content

Commit

Permalink
Use quote_sub for trivial defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmari authored and Arthur Axel 'fREW' Schmidt committed Sep 22, 2012
1 parent 0fb5858 commit 68d7520
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 47 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ my $deps = {
'XML::Writer' => '0.500',
'Moo' => '1.000003',
'Package::Variant' => '1.001001',
'Sub::Quote' => '0',
'Try::Tiny' => '0.04',
},
recommends => {
Expand Down
11 changes: 6 additions & 5 deletions lib/SQL/Translator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use File::Find;
use File::Spec::Functions qw(catfile);
use File::Basename qw(dirname);
use IO::Dir;
use Sub::Quote qw(quote_sub);
use SQL::Translator::Producer;
use SQL::Translator::Schema;
use SQL::Translator::Utils qw(throw ex2err);
Expand Down Expand Up @@ -90,15 +91,15 @@ sub BUILD {

has $_ => (
is => 'rw',
default => sub { 0 },
default => quote_sub(q{ 0 }),
coerce => sub { $_[0] ? 1 : 0 },
) foreach qw(add_drop_table no_comments show_warnings trace validate);

# quote_identifiers is on by default, use a 0-but-true as indicator
# so we can allow individual producers to change the default
has quote_identifiers => (
is => 'rw',
default => sub { '0E0' },
default => quote_sub(q{ '0E0' }),
coerce => sub { $_[0] || 0 },
);

Expand Down Expand Up @@ -137,7 +138,7 @@ around producer => sub {

has producer_type => ( is => 'rwp', init_arg => undef );

has producer_args => ( is => 'rw', default => sub { +{} } );
has producer_args => ( is => 'rw', default => quote_sub(q{ +{} }) );

around producer_args => sub {
my $orig = shift;
Expand All @@ -158,7 +159,7 @@ around parser => sub {

has parser_type => ( is => 'rwp', init_arg => undef );

has parser_args => ( is => 'rw', default => sub { +{} } );
has parser_args => ( is => 'rw', default => quote_sub(q{ +{} }) );

around parser_args => sub {
my $orig = shift;
Expand All @@ -167,7 +168,7 @@ around parser_args => sub {

has filters => (
is => 'rw',
default => sub { [] },
default => quote_sub(q{ [] }),
coerce => sub {
my @filters;
# Set. Convert args to list of [\&code,@args]
Expand Down
3 changes: 2 additions & 1 deletion lib/SQL/Translator/Role/Error.pm
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package SQL::Translator::Role::Error;
use Moo::Role;
use Sub::Quote qw(quote_sub);

has _ERROR => (
is => 'rw',
accessor => 'error',
init_arg => undef,
default => sub { '' },
default => quote_sub(q{ '' }),
);

around error => sub {
Expand Down
3 changes: 2 additions & 1 deletion lib/SQL/Translator/Role/ListAttr.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package SQL::Translator::Role::ListAttr;
use strictures 1;
use SQL::Translator::Utils qw(parse_list_arg ex2err);
use List::MoreUtils qw(uniq);
use Sub::Quote qw(quote_sub);

use Package::Variant (
importing => {
Expand All @@ -24,7 +25,7 @@ sub make_variant {
has($name => (
is => 'rw',
(!$arguments{builder} ? (
default => sub { [] },
default => quote_sub(q{ [] }),
) : ()),
coerce => $coerce,
%arguments,
Expand Down
17 changes: 9 additions & 8 deletions lib/SQL/Translator/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use SQL::Translator::Schema::Procedure;
use SQL::Translator::Schema::Table;
use SQL::Translator::Schema::Trigger;
use SQL::Translator::Schema::View;
use Sub::Quote qw(quote_sub);

use SQL::Translator::Utils 'parse_list_arg';
use Carp;
Expand All @@ -41,12 +42,12 @@ extends 'SQL::Translator::Schema::Object';
our $VERSION = '1.59';


has _order => (is => 'ro', default => sub { +{ map { $_ => 0 } qw/
has _order => (is => 'ro', default => quote_sub(q{ +{ map { $_ => 0 } qw/
table
view
trigger
proc
/} },
/} }),
);

# FIXME - to be removed, together with the SQL::Translator::Schema::Graph* stuff
Expand Down Expand Up @@ -96,7 +97,7 @@ Returns a Graph::Directed object with the table names for nodes.
return $g;
}

has _tables => ( is => 'ro', init_arg => undef, default => sub { +{} } );
has _tables => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) );

sub add_table {

Expand Down Expand Up @@ -189,7 +190,7 @@ can be set to 1 to also drop all triggers on the table, default is 0.
return $table;
}

has _procedures => ( is => 'ro', init_arg => undef, default => sub { +{} } );
has _procedures => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) );

sub add_procedure {

Expand Down Expand Up @@ -275,7 +276,7 @@ object.
return $proc;
}

has _triggers => ( is => 'ro', init_arg => undef, default => sub { +{} } );
has _triggers => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) );

sub add_trigger {

Expand Down Expand Up @@ -358,7 +359,7 @@ trigger name or an C<SQL::Translator::Schema::Trigger> object.
return $trigger;
}

has _views => ( is => 'ro', init_arg => undef, default => sub { +{} } );
has _views => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) );

sub add_view {

Expand Down Expand Up @@ -447,7 +448,7 @@ Get or set the schema's database. (optional)
=cut

has database => ( is => 'rw', default => sub { '' } );
has database => ( is => 'rw', default => quote_sub(q{ '' }) );

sub is_valid {

Expand Down Expand Up @@ -743,7 +744,7 @@ Get or set the schema's name. (optional)
=cut

has name => ( is => 'rw', default => sub { '' } );
has name => ( is => 'rw', default => quote_sub(q{ '' }) );

=pod
Expand Down
17 changes: 9 additions & 8 deletions lib/SQL/Translator/Schema/Constraint.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use SQL::Translator::Schema::Constants;
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);

extends 'SQL::Translator::Schema::Object';

Expand Down Expand Up @@ -87,7 +88,7 @@ False, so the following are eqivalent:
=cut

has deferrable => ( is => 'rw', coerce => sub { $_[0] ? 1 : 0 }, default => sub { 1 } );
has deferrable => ( is => 'rw', coerce => sub { $_[0] ? 1 : 0 }, default => quote_sub(q{ 1 }) );

=head2 expression
Expand All @@ -97,7 +98,7 @@ Gets and set the expression used in a CHECK constraint.
=cut

has expression => ( is => 'rw', default => sub { '' } );
has expression => ( is => 'rw', default => quote_sub(q{ '' }) );

around expression => sub {
my ($orig, $self, $arg) = @_;
Expand Down Expand Up @@ -220,7 +221,7 @@ Get or set the constraint's match_type. Only valid values are "full"

has match_type => (
is => 'rw',
default => sub { '' },
default => quote_sub(q{ '' }),
coerce => sub { lc $_[0] },
isa => sub {
my $arg = $_[0];
Expand All @@ -239,7 +240,7 @@ Get or set the constraint's name.
=cut

has name => ( is => 'rw', default => sub { '' } );
has name => ( is => 'rw', default => quote_sub(q{ '' }) );

around name => sub {
my ($orig, $self, $arg) = @_;
Expand All @@ -266,7 +267,7 @@ Get or set the constraint's "on delete" action.
=cut

has on_delete => ( is => 'rw', default => sub { '' } );
has on_delete => ( is => 'rw', default => quote_sub(q{ '' }) );

around on_delete => sub {
my ($orig, $self, $arg) = @_;
Expand All @@ -281,7 +282,7 @@ Get or set the constraint's "on update" action.
=cut

has on_update => ( is => 'rw', default => sub { '' } );
has on_update => ( is => 'rw', default => quote_sub(q{ '' }) );

around on_update => sub {
my ($orig, $self, $arg) = @_;
Expand Down Expand Up @@ -338,7 +339,7 @@ Get or set the table referred to by the constraint.
=cut

has reference_table => ( is => 'rw', default => sub { '' } );
has reference_table => ( is => 'rw', default => quote_sub(q{ '' }) );

=head2 table
Expand All @@ -362,7 +363,7 @@ Get or set the constraint's type.

has type => (
is => 'rw',
default => sub { '' },
default => quote_sub(q{ '' }),
isa => sub {
throw("Invalid constraint type: $_[0]")
if $_[0] && !$VALID_CONSTRAINT_TYPE{ $_[0] };
Expand Down
11 changes: 6 additions & 5 deletions lib/SQL/Translator/Schema/Field.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use Moo;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Types qw(schema_obj);
use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
use Sub::Quote qw(quote_sub);

extends 'SQL::Translator::Schema::Object';

Expand Down Expand Up @@ -98,7 +99,7 @@ all the comments joined on newlines.
has comments => (
is => 'rw',
coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] },
default => sub { [] },
default => quote_sub(q{ [] }),
);

around comments => sub {
Expand All @@ -124,7 +125,7 @@ Get or set the field's data type.
=cut

has data_type => ( is => 'rw', default => sub { '' } );
has data_type => ( is => 'rw', default => quote_sub(q{ '' }) );

=head2 sql_data_type
Expand Down Expand Up @@ -273,7 +274,7 @@ foreign keys; checks) are represented as table constraints.
has is_nullable => (
is => 'rw',
coerce => sub { $_[0] ? 1 : 0 },
default => sub { 1 },
default => quote_sub(q{ 1 }),
);

around is_nullable => sub {
Expand Down Expand Up @@ -406,7 +407,7 @@ Get or set the field'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 ) = @_;
Expand Down Expand Up @@ -451,7 +452,7 @@ numbers and returns a string.

has size => (
is => 'rw',
default => sub { [0] },
default => quote_sub(q{ [0] }),
coerce => sub {
my @sizes = grep { defined && m/^\d+(?:\.\d+)?$/ } @{parse_list_arg($_[0])};
@sizes ? \@sizes : [0];
Expand Down
5 changes: 3 additions & 2 deletions lib/SQL/Translator/Schema/Index.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use SQL::Translator::Schema::Constants;
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);

extends 'SQL::Translator::Schema::Object';

Expand Down Expand Up @@ -100,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
Expand Down Expand Up @@ -147,7 +148,7 @@ has type => (
throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
},
coerce => sub { uc $_[0] },
default => sub { 'NORMAL' },
default => quote_sub(q{ 'NORMAL' }),
);

around type => \&ex2err;
Expand Down
9 changes: 5 additions & 4 deletions lib/SQL/Translator/Schema/Procedure.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use Moo;
use SQL::Translator::Utils qw(ex2err);
use SQL::Translator::Role::ListAttr;
use SQL::Translator::Types qw(schema_obj);
use Sub::Quote qw(quote_sub);

extends 'SQL::Translator::Schema::Object';

Expand Down Expand Up @@ -69,7 +70,7 @@ Get or set the procedure's name.
=cut

has name => ( is => 'rw', default => sub { '' } );
has name => ( is => 'rw', default => quote_sub(q{ '' }) );

=head2 sql
Expand All @@ -80,7 +81,7 @@ Get or set the procedure's SQL.
=cut

has sql => ( is => 'rw', default => sub { '' } );
has sql => ( is => 'rw', default => quote_sub(q{ '' }) );

=head2 order
Expand All @@ -103,7 +104,7 @@ Get or set the owner of the procedure.
=cut

has owner => ( is => 'rw', default => sub { '' } );
has owner => ( is => 'rw', default => quote_sub(q{ '' }) );

=head2 comments
Expand All @@ -118,7 +119,7 @@ Get or set the comments on a procedure.
has comments => (
is => 'rw',
coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] },
default => sub { [] },
default => quote_sub(q{ [] }),
);

around comments => sub {
Expand Down
3 changes: 2 additions & 1 deletion lib/SQL/Translator/Schema/Role/Extra.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package SQL::Translator::Schema::Role::Extra;
use Moo::Role;
use Sub::Quote qw(quote_sub);


=head1 Methods
Expand All @@ -24,7 +25,7 @@ Returns a hash or a hashref.
=cut

has extra => ( is => 'rwp', default => sub { +{} } );
has extra => ( is => 'rwp', default => quote_sub(q{ +{} }) );

around extra => sub {
my ($orig, $self) = (shift, shift);
Expand Down
Loading

0 comments on commit 68d7520

Please sign in to comment.