Skip to content

Commit

Permalink
added AsIf::Perl and module ecosystem stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sirrobert committed Aug 23, 2012
1 parent c0d40f3 commit de6e873
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ simple) re-renderings.
**AsIf::*** **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 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`~~). you will get a role called `AsIf::JSON` (rather than ~~`Masquerade::AsIf::JSON`~~).


Expand All @@ -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. Let's look at how we could use Masquerade to emit a json rendering of a hash containing an array.


```perl6 ```perl6
use Masquerade::AsIf::JSON; use Masquerade;


my %agenda = { my %agenda = {
parties => 2, parties => 2,
Expand All @@ -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. 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 ```perl6
use Masquerade::AsIf::JSON; use Masquerade;


## ##
# Try a more complex perl object. # Try a more complex perl object.
Expand Down Expand Up @@ -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. 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.


















8 changes: 8 additions & 0 deletions lib/Masquerade.pm6
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@

module Masquerade;

use Masquerade::AsIf::JSON;
use Masquerade::AsIf::Perl;



20 changes: 20 additions & 0 deletions lib/Masquerade/AsIf/Perl.pm6
Original file line number Original file line Diff line number Diff line change
@@ -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];
}


2 changes: 1 addition & 1 deletion t/asif-json/render.t → t/asif-json.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Test;
plan 3; plan 3;


# Load the module # Load the module
use Masquerade::AsIf::JSON; use Masquerade;


# Predeclare a reusable variable for the expected jsonification. # Predeclare a reusable variable for the expected jsonification.
my $expected; my $expected;
Expand Down
12 changes: 12 additions & 0 deletions t/asif-perl6.t
Original file line number Original file line Diff line number Diff line change
@@ -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.