Skip to content

Commit

Permalink
Add shortcut methods for transform()
Browse files Browse the repository at this point in the history
  • Loading branch information
rwstauner committed Sep 1, 2012
1 parent 8d42a0b commit ba2a214
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Revision history for DBIx::RoboQuery
{{$NEXT}}

- Add 'squeeze_blank_lines' option
- Add 'tr_fields()' and 'tr_groups()' as shortcuts for 'transform()'

0.020 2012-06-29T23:05:40Z

Expand Down
35 changes: 35 additions & 0 deletions lib/DBIx/RoboQuery.pm
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,41 @@ sub transform {
$tr->append(@tr);
}

# shortcuts

=method tr_fields
Shortcut for calling L</transform> on fields.
$query->tr_fields("func", "fld1", "arg1", "arg2");
Is equivalent to
$query->transform("func", fields => "fld1", args => ["arg1", "arg2"]);
The second parameter (the fields) can be either a single string
or an array ref.
=method tr_groups
Just like L</tr_fields> but the second parameter is for groups.
=cut

sub tr_fields {
my ($self, $name, $fields, @args) = @_;
return $self->transform($name, fields => $fields, args => [@args]);
}

sub tr_groups {
my ($self, $name, $groups, @args) = @_;
return $self->transform($name, groups => $groups, args => [@args]);
}

# TODO: tr_row? could do it named func style,
# but a template of '[% row.fld1 = 0 %]' seems more useful
# (updating the hash should work, but if not this would: '[% _save_row(row) %]')

1;

=for stopwords TODO arrayrefs
Expand Down
14 changes: 13 additions & 1 deletion t/query.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ my @templates = (
{sql => qq|[% CALL query.transform('trim', 'fields', ['address']); GET query.transformations.queue.first.first %]|, transformations => $transformations},
qq|trim|,
],
[
{sql => qq|[% CALL query.tr_fields('trim', 'address'); GET query.transformations.queue.first.first %]|, transformations => $transformations},
qq|trim|,
],
[
{sql => $cond_while},
qq|1 WHERE account_number LIKE '%D001%' OR account_number LIKE '%D002%' |,
Expand Down Expand Up @@ -144,11 +148,19 @@ foreach my $template ( @templates ){
is($q->sql, '12hi', 'sql');
is_deeply($i, 12, 'only process once');

$q = $mod->new(sql => qq|[% CALL query.transform('trim', 'fields', 'help') %]hi|, transformations => $transformations);
foreach my $sql (
qq|[% CALL query.transform('trim', 'fields', 'help') %]hi|,
qq|[% CALL query.tr_fields('trim', 'help') %]hi|,
qq|[% CALL query.transform('trim', 'groups', 'helpful') %]hi|,
qq|[% CALL query.tr_groups('trim', 'helpful') %]hi|,
){
$q = $mod->new(sql => $sql, transformations => $transformations);
$q->{transformations}->group(helpful => ['help']);
is($q->sql, 'hi', 'sql');
is(scalar @{$q->{transformations}->{queue}}, 1, 'only process once');
is($q->sql, 'hi', 'sql');
is(scalar @{$q->{transformations}->{queue}}, 1, 'only process once');
}
}

isa_ok($mod->new(sql => "hi.")->resultset, 'DBIx::RoboQuery::ResultSet');
Expand Down
4 changes: 2 additions & 2 deletions t/resultset.t
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ my $trdatatree = {
my $arraysfromhashes = sub { [map { [@$_{qw(id hello name)}] } @_] };

$query->transform('trim', groups => 'non_key');
$query->transform('squeeze', groups => 'non_key');
$query->transform('uc', fields => [qw(id name)]);
$query->tr_groups('squeeze', 'non_key');
$query->tr_fields('uc', [qw(id name)]);

# reset dbh, sth
$mock_sth->{NAME_lc} = [qw(id hello name)];
Expand Down

0 comments on commit ba2a214

Please sign in to comment.