Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Some bug fixes and modifications to how the date: modifier works.

  • Loading branch information...
commit f8e5ea2a004592f98abd69979c0e8332e94c3b6a 1 parent 031c66b
@supernovus authored
Showing with 45 additions and 18 deletions.
  1. +24 −10 lib/Flower.pm
  2. +4 −1 lib/Flower/Utils/Date.pm
  3. +17 −7 t/06-utils-date.t
View
34 lib/Flower.pm
@@ -342,8 +342,8 @@ method process-query($data is copy, :$forcexml, :$noxml, :$noescape, :$bool) {
## the value represents an action to take, if it is 0, then no querying or
## parsing is done on the value. If it is 1, then the value is parsed as a
## string with any ${name} variables queried.
-## If you set :query to the string 'string' then ALL parameters will be
-## parsed as strings (replacing any ${name} vars in the strings).
+## If there is a key called .STRING in the query Hash, then parsing as
+## strings becomes default, and keys with a value of 1 parse as normal queries.
## so :query({0=>0, 3=>0}) would query all parameters except the 1st and 4th.
## If you specify the :named option, it will always include the %named
## parameter, even if it's empty.
@@ -387,19 +387,30 @@ method get-args($string, :$query, :$named, *@defaults) {
}
method !parse-rules ($rules, $tag, $value) {
+ my $stringy = False;
+ if $rules ~~ Hash && $rules.exists('.STRING') {
+ $stringy = True;
+ }
if $rules ~~ Hash && $rules.exists($tag) {
if $rules{$tag} {
- return self.parse-string($value);
+ if $stringy {
+ return self.query($value);
+ }
+ else {
+ return self.parse-string($value);
+ }
}
else {
return $value;
}
}
- elsif $rules ~~ Str && $rules eq 'string' {
- return self.parse-string($value);
- }
else {
- return self.query($value);
+ if $stringy {
+ return self.parse-string($value);
+ }
+ else {
+ return self.query($value);
+ }
}
}
@@ -463,7 +474,7 @@ method add-modifier($name, Callable $routine) {
##
## Now $flower has all Logic modifiers, plus 'sort'.
##
-multi method add-modifiers(%modifiers) {
+method add-modifiers(%modifiers) {
for %modifiers.kv -> $key, $val {
self.add-modifier($key, $val);
}
@@ -482,14 +493,17 @@ multi method add-modifiers(%modifiers) {
##
## Example will load File::Utils::Text, File::Utils::Date and My::Modifiers.
##
-multi method load-modifiers(*@modules) {
+method load-modifiers(*@modules) {
for @modules -> $module {
my $plugin = $module;
if $plugin !~~ /'::'/ {
$plugin = "Flower::Utils::$plugin";
}
eval("use $plugin");
- self.add-modifiers(eval($plugin~'::export()'));
+ if defined $! { die "use failed: $!"; }
+ my $modifiers = eval($plugin~'::export()');
+ if defined $! { die "export failed: $!"; }
+ self.add-modifiers($modifiers);
}
}
View
5 lib/Flower/Utils/Date.pm
@@ -21,7 +21,10 @@ our sub export() {
our sub date_new ($parent, $query, *%opts) {
my ($year, $month, $day, $hour, $minute, $second, %params) =
- $parent.get-args(:query, :named, $query, 1, 1, 0, 0, 0);
+ $parent.get-args(
+ :query({'.STRING'=>1, 'tz' => 1}),
+ :named, $query, 1, 1, 0, 0, 0
+ );
if defined $year {
my $timezone = 0;
if %params.exists('tz') && %params<tz> ~~ Str {
View
24 t/06-utils-date.t
@@ -5,7 +5,7 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-plan 6;
+plan 7;
my $xml = '<?xml version="1.0"?>';
@@ -16,11 +16,14 @@ my %date = {
:hour(13), :minute(17), :second(14),
:timezone('16200') # +0430
),
+ 'year' => 2010,
+ 'month' => 10,
+ 'day' => 10,
};
## test 1
-my $template = "<date tal:content=\"date: '2010' '10' '10'\"/>";
+my $template = '<date tal:content="date: 2010 10 10"/>';
my $flower = Flower.new(:template($template));
$flower.load-modifiers('Date');
@@ -29,33 +32,40 @@ is $flower.parse(), $xml~'<date>2010-10-10T00:00:00Z</date>', 'date: modifier';
## test 2
+$template = '<date tal:content="date: ${date/year} ${date/month} ${date/day}"/>';
+$flower.=another(:template($template));
+
+is $flower.parse(:date(%date)), $xml~'<date>2010-10-10T00:00:00Z</date>', 'date: modifier using string parsing';
+
+## test 3
+
$template = '<date tal:content="time: \'1286666133\'"/>';
$flower.=another(:template($template));
is $flower.parse(), $xml~'<date>2010-10-09T23:15:33Z</date>', 'time: modifier';
-## test 3
+## test 4
$template = '<date tal:content="strftime: \'%Y_%m_%d-%T\' date/datetime"/>';
$flower.=another(:template($template));
is $flower.parse(:date(%date)), $xml~'<date>2010_10_11-13:17:14</date>', 'strftime: modifier on a datetime object';
-## test 4
+## test 5
$template = '<date tal:content="strftime: \'%b %d, %Y\' date/date"/>';
$flower.=another(:template($template));
is $flower.parse(:date(%date)), $xml~'<date>Oct 09, 2010</date>', 'strftime: modifier on a date object';
-## test 5
+## test 6
-$template = "<date tal:content=\"strftime: rfc: \{\{date: '2010' '10' '10' :tz('-0800')}}\"/>";
+$template = "<date tal:content=\"strftime: rfc: \{\{date: 2010 10 10 :tz('-0800')}}\"/>";
$flower.=another(:template($template));
is $flower.parse(), $xml~'<date>Sun, 10 Oct 2010 00:00:00 -0800</date>', 'strftime: with rfc: modifier';
-## test 6
+## test 7
$template = '<date tal:content="strftime: \'%Y-%m-%d\' now:"/>';
my $now = Date.today();
Please sign in to comment.
Something went wrong with that request. Please try again.