Permalink
Browse files

AsIf::YAML can now deal with Pairs

  • Loading branch information...
1 parent f344d97 commit 6789ea30843cfade91262b682e09b3ed69017554 @sirrobert committed Aug 29, 2012
Showing with 11 additions and 13 deletions.
  1. +6 −13 lib/Masquerade/AsIf/YAML.pm
  2. +5 −0 t/asif-yaml.t
@@ -13,11 +13,13 @@ sub asif-yaml ($obj) {
# Render as string.
method Str () {
+ warn 'in str';
return asif-yaml(self);
}
# Outsource gist to Str.
method gist () {
+ warn 'in gist';
return self.Str();
}
@@ -37,52 +39,43 @@ sub post-process (Str $str is rw, Int $depth) {
# add it upon request).
multi sub _render(%hash, Int $depth = 0) {
my Str $output;
-
pre-process($output, $depth);
-
my $strlen = 0;
for %hash.keys -> $k {
$strlen = $k.chars if $k.chars > $strlen;
}
-
for %hash.kv -> $k, $v {
$output ~= ' ' x $depth;
$output ~= sprintf("%-{$strlen + 1}s ", "$k:") ~ _render($v, $depth+1);
}
-
post-process($output, $depth);
-
return $output;
}
multi sub _render (@array, Int $depth = 0) {
my Str $output;
-
pre-process($output, $depth);
-
for @array -> $element {
$output ~= ' ' x $depth ~ '- ' ~ _render($element, $depth + 1);
}
-
post-process($output, $depth);
-
return $output;
}
+multi sub _render (Pair $pair, Int $depth=0) {
+ return _render({$pair.key => $pair.value});
+}
+
multi sub _render (Str $str, Int $depth = 0) {
my Str $output;
-
$output ~= "$str\n";
-
return $output;
}
multi sub _render (Numeric $num, Int $depth = 0) {
my Str $output;
-
$output = $num.Str ~ "\n";
-
return $output;
}
View
@@ -23,6 +23,11 @@ my %test-data = {
yaml => "- 1\n- 2\n- 3\n",
},
+ 'array of pairs' => {
+ perl => [a=>1, b=>2, c=>3],
+ yaml => "- a: 1\n- b: 2\n- c: 3\n",
+ },
+
'complex nested: hash array string int float' => {
perl => {

0 comments on commit 6789ea3

Please sign in to comment.