Skip to content

Commit

Permalink
Made get-args support auto-querying, started on Date modifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
supernovus committed Oct 9, 2010
1 parent e6899d3 commit 8caffee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
9 changes: 8 additions & 1 deletion lib/Flower.pm
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ method process-query($data is copy, :$forcexml, :$noxml, :$noescape, :$bool) {
## get-args now supports parameters in the form of ((param name)) for
## when you have queries with spaces in them that shouldn't be treated
## as strings, like 'a string' does.
method get-args($string, *@defaults) {
method get-args($string, :$query, *@defaults) {
my @result = $string.comb(/ [ \'.*?\' | '(('.*?'))' | \S+ ] /);
@result>>.=subst(/^'(('/, '');
@result>>.=subst(/'))'$/, '');
Expand All @@ -342,6 +342,13 @@ method get-args($string, *@defaults) {
if $results < $defs {
@result.push: @defaults[$results..$defs-1];
}
if $query {
for @result -> $result is rw {
if defined $result {
$result = self.query($result);
}
}
}
return @result;
}

Expand Down
21 changes: 21 additions & 0 deletions lib/Flower/Utils/Date.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Flower::Utils::Date;

use DateTime::Utils;

our sub all() {
my %modifiers = {
date => &make_date,
# time => &date_time,
# strftime => &strf_time,
# rfc => &timestamp_rfc,
};
return %modifiers;
}

our sub date($parent, $query, *%opts) {
my ($year, $month, $day, $hour, $minute, $second) =
$parent.get-args(:query, $query, Nil xx 5);
my $dt = DateTime.new($year, $month, $day, $hour, $minute, $second);
return $parent.process-query($dt, |%opts);
}

15 changes: 4 additions & 11 deletions lib/Flower/Utils/Text.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,10 @@ our sub sub_string ($parent, $query, *%opts) {
## E.g.: <div tal:content="printf: '$%0.2f' '2.5'"/>
## Returns: <div>$2.50</div>
our sub print_formatted ($parent, $query, *%opts) {
my ($fmtquery, $subquery) = $parent.get-args($query, Nil);
if defined $subquery {
my $format = $parent.query($fmtquery);
my $text = $parent.query($subquery);
if $text && $format {
my $formatted = sprintf($format, $text);
return $parent.process-query($formatted, |%opts);
}
}
else {
return $parent.process-query($parent.query($query), |%opts);
my ($format, $text) = $parent.get-args(:query, $query, Nil);
if defined $text && defined $format {
my $formatted = sprintf($format, $text);
return $parent.process-query($formatted, |%opts);
}
}

0 comments on commit 8caffee

Please sign in to comment.