From c804300c39626143dea1a4295d178fabe5d7c991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Thu, 16 Aug 2012 15:55:49 +0200 Subject: [PATCH] Use quote_sub for trivial coercions --- lib/SQL/Translator.pm | 4 ++-- lib/SQL/Translator/Role/Debug.pm | 3 ++- lib/SQL/Translator/Schema/Constraint.pm | 6 +++--- lib/SQL/Translator/Schema/Field.pm | 10 +++++----- lib/SQL/Translator/Schema/Index.pm | 4 ++-- lib/SQL/Translator/Schema/Procedure.pm | 2 +- lib/SQL/Translator/Schema/Table.pm | 2 +- lib/SQL/Translator/Schema/Trigger.pm | 4 ++-- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 9e1d0b5b8..9398b3594 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -90,7 +90,7 @@ 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 @@ -98,7 +98,7 @@ has $_ => ( has quote_identifiers => ( is => 'rw', default => quote_sub(q{ '0E0' }), - coerce => sub { $_[0] || 0 }, + coerce => quote_sub(q{ $_[0] || 0 }), ); sub quote_table_names { diff --git a/lib/SQL/Translator/Role/Debug.pm b/lib/SQL/Translator/Role/Debug.pm index 551f30dfa..671e00a8e 100644 --- a/lib/SQL/Translator/Role/Debug.pm +++ b/lib/SQL/Translator/Role/Debug.pm @@ -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, ); diff --git a/lib/SQL/Translator/Schema/Constraint.pm b/lib/SQL/Translator/Schema/Constraint.pm index edf8752bd..e5cb69a8a 100644 --- a/lib/SQL/Translator/Schema/Constraint.pm +++ b/lib/SQL/Translator/Schema/Constraint.pm @@ -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 @@ -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") @@ -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; diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index 8f943147f..a4e411017 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -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{ [] }), ); @@ -201,7 +201,7 @@ Get or set the field's C attribute. has is_auto_increment => ( is => 'rw', - coerce => sub { $_[0] ? 1 : 0 }, + coerce => quote_sub(q{ $_[0] ? 1 : 0 }), builder => 1, lazy => 1, ); @@ -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, ); @@ -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 }), ); @@ -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, ); diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index 99eb38ce3..b238e2711 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -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 @@ -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' }), ); diff --git a/lib/SQL/Translator/Schema/Procedure.pm b/lib/SQL/Translator/Schema/Procedure.pm index e557bac24..1a291e3b7 100644 --- a/lib/SQL/Translator/Schema/Procedure.pm +++ b/lib/SQL/Translator/Schema/Procedure.pm @@ -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{ [] }), ); diff --git a/lib/SQL/Translator/Schema/Table.pm b/lib/SQL/Translator/Schema/Table.pm index b256f6c74..eaae5d060 100644 --- a/lib/SQL/Translator/Schema/Table.pm +++ b/lib/SQL/Translator/Schema/Table.pm @@ -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{ [] }), ); diff --git a/lib/SQL/Translator/Schema/Trigger.pm b/lib/SQL/Translator/Schema/Trigger.pm index 9c037ca15..d643bbac4 100644 --- a/lib/SQL/Translator/Schema/Trigger.pm +++ b/lib/SQL/Translator/Schema/Trigger.pm @@ -72,7 +72,7 @@ C. 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; @@ -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 ];