From: Arthur Axel 'fREW' Schmidt Date: Thu, 24 Feb 2011 00:07:15 +0000 (-0600) Subject: add shim for non-lethal future development X-Git-Tag: v0.11008~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=27ae9ae744f9731e58ff047eda8d02f9b96bc86f;p=dbsrgits%2FSQL-Translator.git add shim for non-lethal future development --- diff --git a/Makefile.PL b/Makefile.PL index 96f385c..d8bc07c 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -22,6 +22,7 @@ my $deps = { 'File::Spec' => 0, 'Scalar::Util' => 0, 'XML::Writer' => 0.500, + 'Moo' => 0.009007, }, recommends => { 'Template' => 2.20, diff --git a/lib/SQL/Translator/Shim.pm b/lib/SQL/Translator/Shim.pm new file mode 100644 index 0000000..b9b3b5f --- /dev/null +++ b/lib/SQL/Translator/Shim.pm @@ -0,0 +1,51 @@ +package SQL::Translator::Shim; + +use Moo; +use Sub::Quote 'quote_sub'; + +# this should be ro, but I have to modify it in BUILD so bleh +has quote_chars => ( is => 'rw' ); + +has name_sep => ( + is => 'ro', + default => quote_sub q{ '.' }, +); + +sub BUILD { + my $self = shift; + + unless (ref($self->quote_chars)) { + if ($self->quote_chars) { + $self->quote_chars([$self->quote_chars]) + } else { + $self->quote_chars([]) + } + } + + $self +} + +sub quote { + my ($self, $label) = @_; + + return '' unless defined $label; + return $$label if ref($label) eq 'SCALAR'; + + my @quote_chars = @{$self->quote_chars}; + return $label unless scalar @quote_chars; + + my ($l, $r); + if (@quote_chars == 1) { + ($l, $r) = (@quote_chars) x 2; + } elsif (@quote_chars == 2) { + ($l, $r) = @quote_chars; + } else { + die 'too many quote chars!'; + } + + my $sep = $self->name_sep || ''; + # parts containing * are naturally unquoted + join $sep, map "$l$_$r", ( $sep ? split (/\Q$sep\E/, $label ) : $label ) +} + +1; diff --git a/t/65-shim.t b/t/65-shim.t new file mode 100644 index 0000000..23545c5 --- /dev/null +++ b/t/65-shim.t @@ -0,0 +1,27 @@ +use strict; +use warnings; + +use Test::More; + +use SQL::Translator::Shim; + +my $shim = SQL::Translator::Shim->new( + quote_chars => ['[', ']'], +); + +is $shim->quote('frew'), '[frew]', 'simple quote works'; +is $shim->quote('people.frew'), '[people].[frew]', 'namespaced quote works'; + +my $single_shim = SQL::Translator::Shim->new( + quote_chars => q(|), +); + +is $single_shim->quote('frew'), '|frew|', 'simple single quote works'; +is $single_shim->quote('people.frew'), '|people|.|frew|', 'namespaced single quote works'; + +my $no_shim = SQL::Translator::Shim->new(); + +is $no_shim->quote('frew'), 'frew', 'simple no quote works'; +is $no_shim->quote('people.frew'), 'people.frew', 'namespaced no quote works'; + +done_testing;