line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::aref_query; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
813420
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
33
|
|
4
|
3
|
|
|
3
|
|
564
|
use Moo; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
18
|
|
5
|
3
|
|
|
3
|
|
909
|
use Catmandu::Fix; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
1182
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.32'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
with 'Catmandu::Fix::Base'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has query => ( |
12
|
|
|
|
|
|
|
is => 'ro', |
13
|
|
|
|
|
|
|
coerce => sub { RDF::aREF::Query->new( query => $_[0] ) } # TODO: ns |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has path => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has subject => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
around 'BUILDARGS', sub { |
25
|
|
|
|
|
|
|
my $orig = shift; |
26
|
|
|
|
|
|
|
my $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
if (@_ == 3) { |
29
|
|
|
|
|
|
|
$orig->($self, subject => $_[0], query => $_[1], path => $_[2] ); |
30
|
|
|
|
|
|
|
} elsif (@_ == 2) { |
31
|
|
|
|
|
|
|
$orig->($self, query => $_[0], path => $_[1] ); |
32
|
|
|
|
|
|
|
} else { |
33
|
|
|
|
|
|
|
$orig->($self, @_); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub emit { |
38
|
5
|
|
|
5
|
0
|
152482
|
my ($self, $fixer) = @_; |
39
|
|
|
|
|
|
|
|
40
|
5
|
100
|
|
|
|
44
|
my $subject = defined $self->subject ? $fixer->emit_string($self->subject) : 'undef'; |
41
|
5
|
|
|
|
|
55
|
my $path = $fixer->split_path($self->path); |
42
|
5
|
|
|
|
|
188
|
my $query = $fixer->capture($self->query); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# TODO: replace/append/single-value mode |
45
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
457
|
my $var = $fixer->var; |
47
|
5
|
|
|
|
|
59
|
my $origin = $fixer->generate_var; |
48
|
5
|
|
|
|
|
200
|
my $values = $fixer->generate_var; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $perl = join "\n", |
51
|
|
|
|
|
|
|
"my ${origin} = ${subject} // ${var}->{_uri} // ${var}->{_url};", |
52
|
|
|
|
|
|
|
"my ${values} = [ ${query}->apply( ${var}, ${origin} ) ];", |
53
|
|
|
|
|
|
|
$fixer->emit_create_path( $var, $path, sub { |
54
|
5
|
|
|
5
|
|
571
|
my $var = shift; |
55
|
5
|
|
|
|
|
50
|
join "\n", map { " $_" } '', |
|
45
|
|
|
|
|
107
|
|
56
|
|
|
|
|
|
|
"if (is_array_ref(${var})) {", |
57
|
|
|
|
|
|
|
" push \@{${var}}, \@{${values}};", |
58
|
|
|
|
|
|
|
"} else {", |
59
|
|
|
|
|
|
|
" if (defined ${var}) {", |
60
|
|
|
|
|
|
|
" unshift \@{${values}}, ${var};", |
61
|
|
|
|
|
|
|
" }", |
62
|
|
|
|
|
|
|
" ${var} = \@{${values}} > 1 ? ${values} : ${values}->[0];", |
63
|
|
|
|
|
|
|
"}" |
64
|
|
|
|
|
|
|
; |
65
|
5
|
|
|
|
|
283
|
}); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# print $perl."\n"; |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
67
|
return $perl; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
__END__ |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 NAME |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Catmandu::Fix::aref_query - copy values of RDF in aREF to a new field |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
In Catmandu Fix language |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
aref_query( dc_title => title ) |
84
|
|
|
|
|
|
|
aref_query( query => 'dc_title', field => 'title' ) |
85
|
|
|
|
|
|
|
aref_query( 'http://example.org/subject', dc_title => title ) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
In Perl code |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use Catmandu::Fix::aref_query as => 'my_query'; |
90
|
|
|
|
|
|
|
use RDF::aREF; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $rdf = encode_aref("example.ttl"); |
93
|
|
|
|
|
|
|
my_query( $rdf, dc_title => 'title' ); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 DESCRIPTION |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
This L<Catmandu::Fix> can be used to map values of imported RDF, given in |
98
|
|
|
|
|
|
|
L<aREF|http://gbv.github.io/aREF/> structure |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 ARGUMENTS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item subject |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Optional subject URI (first argument). By default, the fields C<_uri> and |
107
|
|
|
|
|
|
|
C<_uri> are used. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item query |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
aREF query expression (first or second argument) |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item path |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Field name to map RDF data to (last argument). Existing values are also kept. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=back |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 SEE ALSO |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Function C<aref_query> in L<RDF::aREF> |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |