| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ABSTRACT: document builder - resource |
|
2
|
|
|
|
|
|
|
package PONAPI::Builder::Resource; |
|
3
|
|
|
|
|
|
|
|
|
4
|
28
|
|
|
28
|
|
883943
|
use Moose; |
|
|
28
|
|
|
|
|
1722548
|
|
|
|
28
|
|
|
|
|
180
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
28
|
|
|
28
|
|
198565
|
use PONAPI::Builder::Relationship; |
|
|
28
|
|
|
|
|
96
|
|
|
|
28
|
|
|
|
|
19617
|
|
|
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
|
834
|
my ( $self, $key, $value ) = @_; |
|
51
|
|
|
|
|
|
|
|
|
52
|
348
|
50
|
|
|
|
20715
|
$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
|
|
|
|
|
18825
|
$self->_add_attribute( $key, $value ); |
|
57
|
|
|
|
|
|
|
|
|
58
|
348
|
|
|
|
|
1591
|
return $self; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub add_attributes { |
|
62
|
27
|
|
|
27
|
0
|
5485
|
my ( $self, %args ) = @_; |
|
63
|
27
|
|
|
|
|
189
|
$self->add_attribute( $_, $args{ $_ } ) foreach keys %args; |
|
64
|
27
|
|
|
|
|
237
|
return $self; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub add_relationship { |
|
68
|
136
|
|
|
136
|
0
|
1217
|
my ( $self, $key, $resource, $collection ) = @_; |
|
69
|
|
|
|
|
|
|
|
|
70
|
136
|
50
|
|
|
|
7408
|
$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
|
|
|
|
849
|
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
|
|
|
|
7271
|
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
|
|
|
|
|
818
|
$builder->add_resource( $_ ) foreach @resources; |
|
88
|
|
|
|
|
|
|
|
|
89
|
135
|
|
|
|
|
7282
|
$self->_add_relationship( $key => $builder ); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub add_self_link { |
|
93
|
96
|
|
|
96
|
0
|
172
|
my $self = shift; |
|
94
|
96
|
|
|
|
|
1176
|
my $base = $self->find_root->req_base; |
|
95
|
96
|
|
|
|
|
4513
|
$self->links_builder->add_link( self => $base . $self->{type} . '/' . $self->{id} ); |
|
96
|
96
|
|
|
|
|
1091
|
return $self; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub build { |
|
100
|
378
|
|
|
378
|
0
|
1786
|
my $self = shift; |
|
101
|
378
|
|
|
|
|
847
|
my %args = @_; |
|
102
|
378
|
|
|
|
|
652
|
my $result = {}; |
|
103
|
|
|
|
|
|
|
|
|
104
|
378
|
|
|
|
|
14396
|
$result->{id} = $self->id; |
|
105
|
378
|
|
|
|
|
14325
|
$result->{type} = $self->type; |
|
106
|
378
|
100
|
|
|
|
18785
|
$result->{links} = $self->links_builder->build if $self->has_links_builder; |
|
107
|
378
|
100
|
|
|
|
18481
|
$result->{meta} = $self->_meta if $self->has_meta; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# support filtered output for attributes/relationships through args |
|
110
|
378
|
|
|
|
|
654
|
my @field_filters; |
|
111
|
0
|
|
|
|
|
0
|
@field_filters = @{ $args{fields}{ $self->type } } |
|
112
|
378
|
50
|
33
|
|
|
1394
|
if exists $args{fields} and exists $args{fields}{ $self->type }; |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$result->{attributes} = +{ |
|
115
|
378
|
50
|
|
|
|
19237
|
map { my $v = $self->_get_attribute($_); $v ? ( $_ => $v ) : () } |
|
|
1353
|
50
|
|
|
|
66535
|
|
|
|
1353
|
100
|
|
|
|
5350
|
|
|
116
|
|
|
|
|
|
|
( @field_filters ? @field_filters : $self->_keys_attributes ) |
|
117
|
|
|
|
|
|
|
} if $self->has_attributes; |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$result->{relationships} = +{ |
|
120
|
378
|
50
|
|
|
|
19908
|
map { my $v = $self->_get_relationship($_); $v ? ( $_ => $v->build ) : () } |
|
|
414
|
50
|
|
|
|
21064
|
|
|
|
414
|
100
|
|
|
|
1863
|
|
|
121
|
|
|
|
|
|
|
( @field_filters ? @field_filters : $self->_keys_relationships ) |
|
122
|
|
|
|
|
|
|
} if $self->has_relationships; |
|
123
|
|
|
|
|
|
|
|
|
124
|
378
|
|
|
|
|
1763
|
return $result; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
128
|
28
|
|
|
28
|
|
258
|
no Moose; 1; |
|
|
28
|
|
|
|
|
59
|
|
|
|
28
|
|
|
|
|
226
|
|
|
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.002005 |
|
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 |