| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | MooX::JSON_LD - Extend Moo to provide JSON-LD mark-up for your objects. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Your Moo (or Moose) Class | ||||||||||||||||||||||||||||
| 8 | package My::Moo::Class; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | use Moo; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use MooX::JSON_LD 'Person'; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has first_name => ( | ||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | # various other properties... | ||||||||||||||||||||||||||||
| 17 | json_ld => 1, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | has last_name => ( | ||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | # various other properties... | ||||||||||||||||||||||||||||
| 23 | json_ld => 1, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has birth_date => ( | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | # various other properties... | ||||||||||||||||||||||||||||
| 29 | json_ld => 'birthDate', | ||||||||||||||||||||||||||||
| 30 | json_ld_serializer => sub { shift->birth_date }, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Then, in a program somewhere... | ||||||||||||||||||||||||||||
| 34 | use My::Moo::Class; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $obj = My::Moo::Class->new({ | ||||||||||||||||||||||||||||
| 37 | first_name => 'David', | ||||||||||||||||||||||||||||
| 38 | last_name => 'Bowie', | ||||||||||||||||||||||||||||
| 39 | birth_date => '1947-01-08', | ||||||||||||||||||||||||||||
| 40 | }); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # print a text representation of the JSON-LD | ||||||||||||||||||||||||||||
| 43 | print $obj->json_ld; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # print the raw data structure for the JSON-LD | ||||||||||||||||||||||||||||
| 46 | use Data::Dumper; | ||||||||||||||||||||||||||||
| 47 | print Dumper $obj->json_ld_data; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | This is a companion module for L<MooX::Role::JSON_LD>. It extends the | ||||||||||||||||||||||||||||
| 52 | C<has> method to support options to add attributes to the | ||||||||||||||||||||||||||||
| 53 | C<json_ld_fields> and create the C<json_ld_type> . | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | To declare the type, add it as the option when importing the module, | ||||||||||||||||||||||||||||
| 56 | e.g. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | use MooX::JSON_LD 'Thing'; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Moo attributes are extended with the following options: | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =over | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =item C<json_ld> | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | has headline => ( | ||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||
| 68 | json_ld => 1, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | This adds the "headline" attribute to the C<json_ld_fields>. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | has alt_headline => ( | ||||||||||||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||||||||||
| 75 | json_ld => 'alternateHeadline', | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | This adds the "alt_headline" attribute to the C<json_ld_fields>, with | ||||||||||||||||||||||||||||
| 79 | the label "alternateHeadline". | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item C<json_ld_serializer> | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | has birth_date => ( | ||||||||||||||||||||||||||||
| 84 | is => 'ro', | ||||||||||||||||||||||||||||
| 85 | isa => InstanceOf['DateTime'], | ||||||||||||||||||||||||||||
| 86 | json_ld => 'birthDate', | ||||||||||||||||||||||||||||
| 87 | json_ld_serializer => sub { shift->birth_date->ymd }, | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | This allows you to specify a method for converting the data into an | ||||||||||||||||||||||||||||
| 91 | object that L<JSON> can serialize. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Robert Rothenberg <rrwo@cpan.org> | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | L<MooX::Role::JSON_LD> | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Copyright (C) 2018, Robert Rothenberg. All Rights Reserved. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | This script is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 108 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | package MooX::JSON_LD; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | ▶ | use Moo (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 5 | ▶ | use Moo::Role (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 5 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 5 | ▶ | use List::Util qw/ all /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | 5 | ▶ | use Sub::Quote qw/ quote_sub /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 5 | our $VERSION = '2.1.1'; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 5 | my %Attributes; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 8 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 130 | 8 | my ( $class, $type ) = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 8 | my $target = caller; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 5 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 8 | ▶ | my $installer = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | $target->isa("Moo::Object") | ||||||||||||||||||||||||||||
| 139 | ? \&Moo::_install_tracked | ||||||||||||||||||||||||||||
| 140 | : \&Moo::Role::_install_tracked; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8 | ▶ | if ( my $has = $target->can('has') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 16 | ▶ | my $new_has = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 144 | 16 | $has->( _process_has(@_) ); | |||||||||||||||||||||||||||
| 145 | 8 | }; | |||||||||||||||||||||||||||
| 146 | 8 | $installer->( $target, "has", $new_has ); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 8 | ▶ | if ( defined $type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 8 | quote_sub "${target}::json_ld_type", "'${type}'"; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 8 | my $name = "json_ld_fields"; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | quote_sub "${target}::${name}", '$code->(@_)', | ||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | 25 | ▶ | '$code' => \sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 158 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||
| 159 | 25 | ▶ | my $fields = $self->maybe::next::method || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->maybe::next::method || []
MC/DC100%1/1
$self->maybe::next::method || []
$self->maybe::next::method
Truth table100%2/2
A
A= $self->maybe::next::method
| |||||||||||||||||||||||||||||
| 160 | return [ | ||||||||||||||||||||||||||||
| 161 | 25 | @{$fields}, | |||||||||||||||||||||||||||
| 162 | 25 | ▶ | @{$Attributes{$target} || []} | ||||||||||||||||||||||||||
|
Condition50%1/2
$Attributes{$target} || []
MC/DC0%0/1
$Attributes{$target} || []
$Attributes{$target}
Truth table50%1/2
A
A= $Attributes{$target}
| |||||||||||||||||||||||||||||
| 163 | ]; | ||||||||||||||||||||||||||||
| 164 | }, | ||||||||||||||||||||||||||||
| 165 | }, { | ||||||||||||||||||||||||||||
| 166 | 8 | no_defer => 1, | |||||||||||||||||||||||||||
| 167 | package => $target, | ||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 8 | ▶ | unless ( all { $target->can($_) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | qw/ json_ld_encoder json_ld_data json_ld / ) | ||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 8 | Moo::Role->apply_single_role_to_package( $target, | |||||||||||||||||||||||||||
| 176 | 'MooX::Role::JSON_LD' ); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 16 | ▶ | sub _process_has { | ||||||||||||||||||||||||||
|
Subroutine
_process_has: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||
| 183 | 16 | my ( $name, %opts ) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 16 | ▶ | if ( $opts{json_ld} || $opts{json_ld_serializer} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"json_ld"} or $opts{"json_ld_serializer"}
MC/DC0%0/2
$opts{"json_ld"} or $opts{"json_ld_serializer"}
$opts{"json_ld"}
$opts{"json_ld_serializer"}
Truth table33%1/3
A or B
A= $opts{"json_ld"}B= $opts{"json_ld_serializer"}
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 16 | my $class = caller(1); | |||||||||||||||||||||||||||
| 188 | 16 | ▶ | $Attributes{$class} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$Attributes{$class} ||= []
MC/DC100%1/1
$Attributes{$class} ||= []
$Attributes{$class}
Truth table100%2/2
A
A= $Attributes{$class}
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 16 | my $label = delete $opts{json_ld}; | |||||||||||||||||||||||||||
| 191 | 16 | my $method = delete $opts{json_ld_serializer}; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 16 | ▶ | push @{ $Attributes{$class} }, { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method || $name
MC/DC0%0/2
$method || $name
$method
$name
Truth table66%2/3
A || B
A= $methodB= $name
| |||||||||||||||||||||||||||||
| 194 | $label eq "1" ? $name : $label => $method || $name | ||||||||||||||||||||||||||||
| 195 | }; | ||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 16 | return ( $name, %opts ); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 5 | 1; | |||||||||||||||||||||||||||