line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: document builder - resource |
2
|
|
|
|
|
|
|
package PONAPI::Builder::Resource; |
3
|
|
|
|
|
|
|
|
4
|
28
|
|
|
28
|
|
841372
|
use Moose; |
|
28
|
|
|
|
|
1678624
|
|
|
28
|
|
|
|
|
206
|
|
5
|
|
|
|
|
|
|
|
6
|
28
|
|
|
28
|
|
199301
|
use PONAPI::Builder::Relationship; |
|
28
|
|
|
|
|
110
|
|
|
28
|
|
|
|
|
20564
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'PONAPI::Builder', |
9
|
|
|
|
|
|
|
'PONAPI::Builder::Role::HasLinksBuilder', |
10
|
|
|
|
|
|
|
'PONAPI::Builder::Role::HasMeta'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has id => ( is => 'ro', isa => 'Str', required => 1 ); |
13
|
|
|
|
|
|
|
has type => ( is => 'ro', isa => 'Str', required => 1 ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has _attributes => ( |
16
|
|
|
|
|
|
|
init_arg => undef, |
17
|
|
|
|
|
|
|
traits => [ 'Hash' ], |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
isa => 'HashRef', |
20
|
|
|
|
|
|
|
lazy => 1, |
21
|
|
|
|
|
|
|
default => sub { +{} }, |
22
|
|
|
|
|
|
|
handles => { |
23
|
|
|
|
|
|
|
'has_attributes' => 'count', |
24
|
|
|
|
|
|
|
'has_attribute_for' => 'exists', |
25
|
|
|
|
|
|
|
# private ... |
26
|
|
|
|
|
|
|
'_add_attribute' => 'set', |
27
|
|
|
|
|
|
|
'_get_attribute' => 'get', |
28
|
|
|
|
|
|
|
'_keys_attributes' => 'keys', |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has _relationships => ( |
33
|
|
|
|
|
|
|
init_arg => undef, |
34
|
|
|
|
|
|
|
traits => [ 'Hash' ], |
35
|
|
|
|
|
|
|
is => 'ro', |
36
|
|
|
|
|
|
|
isa => 'HashRef[ PONAPI::Builder::Relationship ]', |
37
|
|
|
|
|
|
|
lazy => 1, |
38
|
|
|
|
|
|
|
default => sub { +{} }, |
39
|
|
|
|
|
|
|
handles => { |
40
|
|
|
|
|
|
|
'has_relationships' => 'count', |
41
|
|
|
|
|
|
|
'has_relationship_for' => 'exists', |
42
|
|
|
|
|
|
|
# private ... |
43
|
|
|
|
|
|
|
'_add_relationship' => 'set', |
44
|
|
|
|
|
|
|
'_get_relationship' => 'get', |
45
|
|
|
|
|
|
|
'_keys_relationships' => 'keys', |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub add_attribute { |
50
|
348
|
|
|
348
|
0
|
777
|
my ( $self, $key, $value ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
348
|
50
|
|
|
|
18783
|
$self->raise_error( 400, |
53
|
|
|
|
|
|
|
title => 'Attribute key conflict, a relation already exists for key: ' . $key |
54
|
|
|
|
|
|
|
) if $self->has_relationship_for( $key ); |
55
|
|
|
|
|
|
|
|
56
|
348
|
|
|
|
|
18235
|
$self->_add_attribute( $key, $value ); |
57
|
|
|
|
|
|
|
|
58
|
348
|
|
|
|
|
1719
|
return $self; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub add_attributes { |
62
|
27
|
|
|
27
|
0
|
4293
|
my ( $self, %args ) = @_; |
63
|
27
|
|
|
|
|
175
|
$self->add_attribute( $_, $args{ $_ } ) foreach keys %args; |
64
|
27
|
|
|
|
|
184
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub add_relationship { |
68
|
136
|
|
|
136
|
0
|
1017
|
my ( $self, $key, $resource, $collection ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
136
|
50
|
|
|
|
7184
|
$self->raise_error( 400, |
71
|
|
|
|
|
|
|
title => 'Relationship key conflict, an attribute already exists for key: ' . $key |
72
|
|
|
|
|
|
|
) if $self->has_attribute_for( $key ); |
73
|
|
|
|
|
|
|
|
74
|
136
|
100
|
|
|
|
897
|
my @resources = |
|
|
100
|
|
|
|
|
|
75
|
|
|
|
|
|
|
( ref $resource eq 'ARRAY' ) ? @$resource : |
76
|
|
|
|
|
|
|
( ref $resource eq 'HASH' ) ? $resource : |
77
|
|
|
|
|
|
|
die 'Relationship resource information must be a reference (HASH or ARRAY)'; |
78
|
|
|
|
|
|
|
|
79
|
135
|
100
|
|
|
|
7412
|
my $builder = $self->has_relationship_for($key) |
80
|
|
|
|
|
|
|
? $self->_get_relationship($key) |
81
|
|
|
|
|
|
|
: PONAPI::Builder::Relationship->new( |
82
|
|
|
|
|
|
|
parent => $self, |
83
|
|
|
|
|
|
|
name => $key, |
84
|
|
|
|
|
|
|
collection => $collection, |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
135
|
|
|
|
|
776
|
$builder->add_resource( $_ ) foreach @resources; |
88
|
|
|
|
|
|
|
|
89
|
135
|
|
|
|
|
7255
|
$self->_add_relationship( $key => $builder ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub add_self_link { |
93
|
96
|
|
|
96
|
0
|
186
|
my $self = shift; |
94
|
96
|
|
|
|
|
418
|
my $base = $self->find_root->req_base; |
95
|
96
|
|
|
|
|
4309
|
$self->links_builder->add_link( self => $base . $self->{type} . '/' . $self->{id} ); |
96
|
96
|
|
|
|
|
990
|
return $self; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub build { |
100
|
378
|
|
|
378
|
0
|
1595
|
my $self = shift; |
101
|
378
|
|
|
|
|
791
|
my %args = @_; |
102
|
378
|
|
|
|
|
661
|
my $result = {}; |
103
|
|
|
|
|
|
|
|
104
|
378
|
|
|
|
|
14025
|
$result->{id} = $self->id; |
105
|
378
|
|
|
|
|
13855
|
$result->{type} = $self->type; |
106
|
378
|
100
|
|
|
|
18365
|
$result->{links} = $self->links_builder->build if $self->has_links_builder; |
107
|
378
|
100
|
|
|
|
18077
|
$result->{meta} = $self->_meta if $self->has_meta; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# support filtered output for attributes/relationships through args |
110
|
378
|
|
|
|
|
586
|
my @field_filters; |
111
|
0
|
|
|
|
|
0
|
@field_filters = @{ $args{fields}{ $self->type } } |
112
|
378
|
50
|
33
|
|
|
1104
|
if exists $args{fields} and exists $args{fields}{ $self->type }; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$result->{attributes} = +{ |
115
|
378
|
50
|
|
|
|
18535
|
map { my $v = $self->_get_attribute($_); $v ? ( $_ => $v ) : () } |
|
1353
|
50
|
|
|
|
63574
|
|
|
1353
|
100
|
|
|
|
5124
|
|
116
|
|
|
|
|
|
|
( @field_filters ? @field_filters : $self->_keys_attributes ) |
117
|
|
|
|
|
|
|
} if $self->has_attributes; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$result->{relationships} = +{ |
120
|
378
|
50
|
|
|
|
19755
|
map { my $v = $self->_get_relationship($_); $v ? ( $_ => $v->build ) : () } |
|
420
|
50
|
|
|
|
20792
|
|
|
420
|
100
|
|
|
|
1829
|
|
121
|
|
|
|
|
|
|
( @field_filters ? @field_filters : $self->_keys_relationships ) |
122
|
|
|
|
|
|
|
} if $self->has_relationships; |
123
|
|
|
|
|
|
|
|
124
|
378
|
|
|
|
|
1642
|
return $result; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
128
|
28
|
|
|
28
|
|
268
|
no Moose; 1; |
|
28
|
|
|
|
|
59
|
|
|
28
|
|
|
|
|
228
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
__END__ |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=pod |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=encoding UTF-8 |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 NAME |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
PONAPI::Builder::Resource - document builder - resource |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 VERSION |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
version 0.002006 |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 AUTHORS |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over 4 |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Mickey Nasriachi <mickey@cpan.org> |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Stevan Little <stevan@cpan.org> |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Brian Fraser <hugmeir@cpan.org> |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=back |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
167
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |