From: Ash Berlin Date: Sun, 22 Feb 2009 20:38:59 +0000 (+0000) Subject: Initial commit X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a0185af2516e75e677b621269c6ce43e0997996a;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Initial commit --- diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..a32a5fb --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,12 @@ +use strict; +use warnings; + +use inc::Module::Install 0.79; + +requires 'Moose' => '0.71'; +requires 'MooseX::Method::Signatures' => '0.07'; +requires 'MooseX::Declare' => '0.04'; + +test_requires 'Test::More'; + +WriteAll; diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm new file mode 100644 index 0000000..c5eafff --- /dev/null +++ b/lib/SQL/Abstract.pm @@ -0,0 +1,70 @@ +use MooseX::Declare; +use MooseX::Method::Signatures; + + +class SQL::Abstract { + + use Carp qw/croak/; + use Data::Dump qw/pp/; + + use Moose::Util::TypeConstraints; + use MooseX::Types -declare => ['NameSeparator']; + use MooseX::Types::Moose qw/ArrayRef Str/; + + subtype NameSeparator, + as ArrayRef[Str]; + #where { @$_ == 1 ||| @$_ == 2 }, + #message { "Name separator must be one or two elements" }; + + coerce NameSeparator, from Str, via { [ $_ ] }; + + our $VERSION = '2.000000'; + + our $AST_VERSION = '1'; + + has name_separator => ( + is => 'rw', + isa => NameSeparator, + default => sub { ['.'] }, + coerece => 1, + required => 1, + ); + + has list_separator => ( + is => 'rw', + isa => Str, + default => ', ', + required => 1, + ); + + method generate (ArrayRef $ast) { + $self = new $self unless blessed($self); + + local $_ = $ast->[0]; + s/^-/_/ or croak "Unknown type tag '$_'"; + return $self->$_($ast); + } + + method _name(ArrayRef[Str] $ast) { + my (undef, @names) = @$ast; + + my $sep = $self->name_separator; + + return $sep->[0] . + join( $sep->[1] . $sep->[0], @names ) . + $sep->[1] + if (@$sep > 1); + + return join($sep->[0], @names); + } + + method _list(ArrayRef $ast) { + my (undef, @items) = @$ast; + + return join( + $self->list_separator, + map { $self->generate($_) } @items); + + } + +}; diff --git a/t/001_basic.t b/t/001_basic.t new file mode 100644 index 0000000..9a6dc39 --- /dev/null +++ b/t/001_basic.t @@ -0,0 +1,18 @@ +use strict; +use warnings; + +use Test::More tests => 3; + +use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); + +is SQL::Abstract->generate( [ -name => qw/me id/]), "me.id", + "Simple name generator"; + +is SQL::Abstract->generate( + [ -list => + [ -name => qw/me id/], + [ -name => qw/me foo bar/], + [ -name => qw/bar/] + ] +), "me.id, me.foo.bar, bar", + "List generator";