From: Justin Hunter Date: Wed, 30 Sep 2009 00:26:06 +0000 (-0700) Subject: allow setting of fields (columns) like ->fields('foo,bar') or ->fields([ qw/foo bar/ ]) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=52137c8d1aee7154786de27d89508c69e6f3b575;p=dbsrgits%2FSQL-Translator-2.0-ish.git allow setting of fields (columns) like ->fields('foo,bar') or ->fields([ qw/foo bar/ ]) --- diff --git a/lib/SQL/Translator/Object/Index.pm b/lib/SQL/Translator/Object/Index.pm index 865503a..41ff945 100644 --- a/lib/SQL/Translator/Object/Index.pm +++ b/lib/SQL/Translator/Object/Index.pm @@ -1,6 +1,7 @@ use MooseX::Declare; class SQL::Translator::Object::Index extends SQL::Translator::Object { use MooseX::Types::Moose qw(HashRef Str); + use MooseX::MultiMethods; use SQL::Translator::Types qw(Column Table); has 'table' => ( @@ -29,7 +30,6 @@ class SQL::Translator::Object::Index extends SQL::Translator::Object { ## compat get_fields => 'values', - fields => 'keys', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); @@ -43,5 +43,19 @@ class SQL::Translator::Object::Index extends SQL::Translator::Object { around add_column(Column $column) { $self->$orig($column->name, $column) } + multi method fields(Str $columns) { + for (split /,/, $columns) { die "Column $_ does not exist!" unless $self->table->exists_column($_) } + $self->add_column($self->table->get_column($_)) for split /,/, $columns; + $self->column_ids; + } + + multi method fields(@columns) { + for (@columns) { die "Column $_ does not exist!" unless $self->table->exists_column($_) } + $self->add_column($self->table->get_column($_)) for @columns; + $self->column_ids; + } + + multi method fields(Any $) { $self->column_ids } + method is_valid { $self->table && scalar $self->get_columns ? 1 : undef } }