Skip to content

Commit

Permalink
Use quote_sub for trivial coercions
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 2bdef63 commit c804300
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/SQL/Translator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ sub BUILD {
has $_ => (
is => 'rw',
default => quote_sub(q{ 0 }),
coerce => sub { $_[0] ? 1 : 0 },
coerce => quote_sub(q{ $_[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 => quote_sub(q{ '0E0' }),
coerce => sub { $_[0] || 0 },
coerce => quote_sub(q{ $_[0] || 0 }),
);

sub quote_table_names {
Expand Down
3 changes: 2 additions & 1 deletion lib/SQL/Translator/Role/Debug.pm
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package SQL::Translator::Role::Debug;
use Moo::Role;
use Sub::Quote qw(quote_sub);

has _DEBUG => (
is => 'rw',
accessor => 'debugging',
init_arg => 'debugging',
coerce => sub { $_[0] ? 1 : 0 },
coerce => quote_sub(q{ $_[0] ? 1 : 0 }),
lazy => 1,
builder => 1,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/SQL/Translator/Schema/Constraint.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ False, so the following are eqivalent:
=cut

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

=head2 expression
Expand Down Expand Up @@ -222,7 +222,7 @@ Get or set the constraint's match_type. Only valid values are "full"
has match_type => (
is => 'rw',
default => quote_sub(q{ '' }),
coerce => sub { lc $_[0] },
coerce => quote_sub(q{ lc $_[0] }),
isa => sub {
my $arg = $_[0];
throw("Invalid match type: $arg")
Expand Down Expand Up @@ -368,7 +368,7 @@ has type => (
throw("Invalid constraint type: $_[0]")
if $_[0] && !$VALID_CONSTRAINT_TYPE{ $_[0] };
},
coerce => sub { (my $t = $_[0]) =~ s/_/ /g; uc $t },
coerce => quote_sub(q{ (my $t = $_[0]) =~ s/_/ /g; uc $t }),
);

around type => \&ex2err;
Expand Down
10 changes: 5 additions & 5 deletions lib/SQL/Translator/Schema/Field.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ all the comments joined on newlines.

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

Expand Down Expand Up @@ -201,7 +201,7 @@ Get or set the field's C<is_auto_increment> attribute.

has is_auto_increment => (
is => 'rw',
coerce => sub { $_[0] ? 1 : 0 },
coerce => quote_sub(q{ $_[0] ? 1 : 0 }),
builder => 1,
lazy => 1,
);
Expand Down Expand Up @@ -232,7 +232,7 @@ Returns whether or not the field is a foreign key.

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

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

Expand All @@ -294,7 +294,7 @@ a table constraint (should it?).

has is_primary_key => (
is => 'rw',
coerce => sub { $_[0] ? 1 : 0 },
coerce => quote_sub(q{ $_[0] ? 1 : 0 }),
lazy => 1,
builder => 1,
);
Expand Down
4 changes: 2 additions & 2 deletions lib/SQL/Translator/Schema/Index.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Get or set the index's name.
=cut

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

=head2 options
Expand Down Expand Up @@ -147,7 +147,7 @@ has type => (
my $type = uc $_[0] or return;
throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
},
coerce => sub { uc $_[0] },
coerce => quote_sub(q{ uc $_[0] }),
default => quote_sub(q{ 'NORMAL' }),
);

Expand Down
2 changes: 1 addition & 1 deletion lib/SQL/Translator/Schema/Procedure.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Get or set the comments on a procedure.

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

Expand Down
2 changes: 1 addition & 1 deletion lib/SQL/Translator/Schema/Table.pm
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ all the comments joined on newlines.

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

Expand Down
4 changes: 2 additions & 2 deletions lib/SQL/Translator/Schema/Trigger.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ C<database_event>.

has perform_action_when => (
is => 'rw',
coerce => sub { defined $_[0] ? lc $_[0] : $_[0] },
coerce => quote_sub(q{ defined $_[0] ? lc $_[0] : $_[0] }),
isa => sub {
throw("Invalid argument '$_[0]' to perform_action_when")
if defined $_[0] and $_[0] !~ m/^(before|after)$/i;
Expand Down Expand Up @@ -106,7 +106,7 @@ Gets or sets the events that triggers the trigger.

has database_events => (
is => 'rw',
coerce => sub { [ map { lc } ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]) ] },
coerce => quote_sub(q{ [ map { lc } ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]) ] }),
isa => sub {
my @args = @{$_[0]};
my %valid = map { $_, 1 } qw[ insert update update_on delete ];
Expand Down

0 comments on commit c804300

Please sign in to comment.