Permalink
Browse files

added AsIf::Perl and module ecosystem stuff

  • Loading branch information...
1 parent c0d40f3 commit de6e873f709058b5539cddb39d884bd8575259c9 @sirrobert committed Aug 23, 2012
Showing with 80 additions and 4 deletions.
  1. +39 −3 README.md
  2. +8 −0 lib/Masquerade.pm6
  3. +20 −0 lib/Masquerade/AsIf/Perl.pm6
  4. +1 −1 t/{asif-json/render.t → asif-json.t}
  5. +12 −0 t/asif-perl6.t
View
42 README.md
@@ -13,7 +13,7 @@ simple) re-renderings.
**AsIf::***
Masquerade provides a set of roles. Each role allows objects to masquerade as if other objects. The roles do 'not' have the `Masquerade` prefix. For example, if you
```
-use Masquerade::AsIf::JSON;
+use Masquerade;
```
you will get a role called `AsIf::JSON` (rather than ~~`Masquerade::AsIf::JSON`~~).
@@ -26,7 +26,7 @@ Sometimes you have objects you want to render differently.
Let's look at how we could use Masquerade to emit a json rendering of a hash containing an array.
```perl6
-use Masquerade::AsIf::JSON;
+use Masquerade;
my %agenda = {
parties => 2,
@@ -43,7 +43,7 @@ say %agenda but AsIf::JSON;
That's well and good, but what about more complex stuff? The following example shows how to render a class instance as if it were JSON.
```perl6
-use Masquerade::AsIf::JSON;
+use Masquerade;
##
# Try a more complex perl object.
@@ -84,3 +84,39 @@ say $tornado but AsIf::JSON;
Notice that the private property $!id is not rendered in the JSON. The JSON is considered to be a JSON rendering of the public interface to the object.
+On the other hand, sometimes you want to go the other way. You can also
+extract useful bits out of things (only JSON is supported right now) as if
+they were Perl.
+
+The following examples let you access JSON strings as if it were Perl:
+
+```perl6
+
+use Masquerade;
+
+my $json = '{"foo": "bar"}';
+
+my $value = ($json but AsIf::Perl)<foo>;
+
+say $value; # bar
+```
+
+This is a read-only operation right now.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
View
8 lib/Masquerade.pm6
@@ -0,0 +1,8 @@
+
+module Masquerade;
+
+use Masquerade::AsIf::JSON;
+use Masquerade::AsIf::Perl;
+
+
+
View
20 lib/Masquerade/AsIf/Perl.pm6
@@ -0,0 +1,20 @@
+# JSON::Tiny is used to render basic stuff like hashes.
+use JSON::Tiny;
+
+role AsIf::Perl;
+
+# Tries to interpret the thing you're doing as a JSON object string.
+# Obviously this won't work if it's not one, so don't do that.
+multi method postcircumfix:<{ }> (Str $str) {
+ my %hash = from-json(self);
+ return %hash{$str};
+}
+
+# Tries to interpret the thing you're doing as a JSON array string.
+# Obviously this won't work if it's not a JSON string, so don't do that.
+multi method postcircumfix:<[ ]> (Int $num) {
+ my $array = from-json(self);
+ return $array[$num];
+}
+
+
View
2 t/asif-json/render.t → t/asif-json.t
@@ -3,7 +3,7 @@ use Test;
plan 3;
# Load the module
-use Masquerade::AsIf::JSON;
+use Masquerade;
# Predeclare a reusable variable for the expected jsonification.
my $expected;
View
12 t/asif-perl6.t
@@ -0,0 +1,12 @@
+use v6;
+use Test;
+plan 2;
+
+# Load the module
+use Masquerade;
+
+# At this point this is pretty simple: just take a json object and turn it
+# into a corresponding perl6 object.
+ok (('{"a":"foo"}' but AsIf::Perl)<a> eq 'foo'), "can access object elements";
+ok (('["a","b","c"]' but AsIf::Perl)[0] eq 'a'), "can access array elements";
+

0 comments on commit de6e873

Please sign in to comment.