Skip to content

Commit

Permalink
Merge pull request #5 from robrwo/rrwo/subclassing
Browse files Browse the repository at this point in the history
Update MooX::JSON_LD to handle subclassing
  • Loading branch information
davorg committed May 28, 2018
2 parents 2cacd14 + 644419e commit 0ccb904
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changes.md
Expand Up @@ -6,6 +6,9 @@

- Added namespace::autoclean

- Fix handling of subclasses in MooX::JSON_LD. It will inherit fields
defined in parent classes.

## [0.0.10] - 2018-05-28

### Fixed
Expand Down
22 changes: 19 additions & 3 deletions lib/MooX/JSON_LD.pm
Expand Up @@ -107,6 +107,7 @@ use warnings;
use Moo ();
use Moo::Role ();

use List::Util qw/ any /;
use Sub::Quote qw/ quote_sub /;

our $VERSION = '0.0.11';
Expand Down Expand Up @@ -137,8 +138,23 @@ sub import {
quote_sub "${target}::json_ld_type", "'${type}'";
}

quote_sub "${target}::json_ld_fields",
__PACKAGE__ . "::_json_ld_fields('${target}',\@_)";
my $name = "json_ld_fields";

quote_sub "${target}::${name}", '$code->(@_)',
{
'$code' => \sub {
my ($self) = @_;
my $fields = eval { $self->next::method };
return [
@{$fields || []},
@{$Attributes{$target} || []}
];
},
}, {
no_defer => 1,
package => $target,
};


Moo::Role->apply_single_role_to_package( $target, 'MooX::Role::JSON_LD' );

Expand Down Expand Up @@ -170,7 +186,7 @@ sub _process_has {

sub _json_ld_fields {
my ( $class, $self ) = @_;
$Attributes{$class} ;
$Attributes{$class} // [];
}

1;
Expand Down
38 changes: 38 additions & 0 deletions t/04-moox-json-subclass.t
@@ -0,0 +1,38 @@
use strict;
use warnings;

use FindBin '$Bin';
use lib "$Bin/lib";

use Test::More;
use MooTester3;

ok(my $obj = MooTester3->new);
isa_ok($obj, 'MooTester3' );
can_ok($obj, qw[ foo bar boop json_ld_type json_ld_fields
json_ld_data json_ld json_ld_encoder ]);

is($obj->foo, 'Foo', 'foo is Foo');
is($obj->bar, 'Bar', 'bar is Bar');
is($obj->boop, 'Bop!', 'boop is Bop!');

is_deeply($obj->json_ld_data, {
'@type' => 'Another',
'@context' => 'http://schema.org/',
bax => 'Bar',
baz => 'Bar Foo',
boop => 'Bop!',
zip => 'Pow',
}, 'JSON data is correct');

is($obj->json_ld, '{
"@context" : "http://schema.org/",
"@type" : "Another",
"bax" : "Bar",
"baz" : "Bar Foo",
"boop" : "Bop!",
"zip" : "Pow"
}
', 'JSON is correct');

done_testing;
17 changes: 17 additions & 0 deletions t/lib/MooTester3.pm
@@ -0,0 +1,17 @@
package MooTester3;

use Moo;
extends 'MooTester2';

use MooX::JSON_LD 'Another';

use namespace::autoclean;

has zip => (
is => 'ro',
default => 'Pow',
json_ld => 1,
);


1;

0 comments on commit 0ccb904

Please sign in to comment.