Skip to content
This repository
Browse code

AsIf::YAML can now deal with Pairs

  • Loading branch information...
commit 6789ea30843cfade91262b682e09b3ed69017554 1 parent f344d97
Sir Robert Burbridge authored
19  lib/Masquerade/AsIf/YAML.pm
@@ -13,11 +13,13 @@ sub asif-yaml ($obj) {
13 13
 
14 14
 # Render as string.
15 15
 method Str () {
  16
+  warn 'in str';
16 17
   return asif-yaml(self);
17 18
 }
18 19
 
19 20
 # Outsource gist to Str.
20 21
 method gist () {
  22
+  warn 'in gist';
21 23
   return self.Str();
22 24
 }
23 25
 
@@ -37,52 +39,43 @@ sub post-process (Str $str is rw, Int $depth) {
37 39
 # add it upon request).
38 40
 multi sub _render(%hash, Int $depth = 0) {
39 41
   my Str $output;
40  
-
41 42
   pre-process($output, $depth);
42  
-
43 43
   my $strlen = 0;
44 44
   for %hash.keys -> $k {
45 45
     $strlen = $k.chars if $k.chars > $strlen;
46 46
   }
47 47
 
48  
-
49 48
   for %hash.kv -> $k, $v {
50 49
     $output ~= '  ' x $depth;
51 50
     $output ~= sprintf("%-{$strlen + 1}s ", "$k:") ~ _render($v, $depth+1);
52 51
   }
53  
-
54 52
   post-process($output, $depth);
55  
-
56 53
   return $output;
57 54
 }
58 55
 
59 56
 multi sub _render (@array, Int $depth = 0) {
60 57
   my Str $output;
61  
-
62 58
   pre-process($output, $depth);
63  
-
64 59
   for @array -> $element {
65 60
     $output ~= '  ' x $depth ~ '- ' ~ _render($element, $depth + 1);
66 61
   }
67  
-
68 62
   post-process($output, $depth);
69  
-
70 63
   return $output;
71 64
 }
72 65
 
  66
+multi sub _render (Pair $pair, Int $depth=0) {
  67
+  return _render({$pair.key => $pair.value});
  68
+}
  69
+
73 70
 multi sub _render (Str $str, Int $depth = 0) {
74 71
   my Str $output;
75  
-
76 72
   $output ~= "$str\n";
77  
-
78 73
   return $output;
79 74
 }
80 75
 
81 76
 multi sub _render (Numeric $num, Int $depth = 0) {
82 77
   my Str $output;
83  
-
84 78
   $output = $num.Str ~ "\n";
85  
-
86 79
   return $output;
87 80
 }
88 81
 
5  t/asif-yaml.t
@@ -23,6 +23,11 @@ my %test-data = {
23 23
     yaml => "- 1\n- 2\n- 3\n",
24 24
   },
25 25
 
  26
+  'array of pairs' => {
  27
+    perl => [a=>1, b=>2, c=>3],
  28
+    yaml => "- a: 1\n- b: 2\n- c: 3\n",
  29
+  },
  30
+  
26 31
 
27 32
   'complex nested: hash array string int float' => {
28 33
     perl => {

0 notes on commit 6789ea3

Please sign in to comment.
Something went wrong with that request. Please try again.