File Coverage

blib/lib/RDF/Lazy/Node.pm
Criterion Covered Total %
statement 67 69 97.1
branch 23 32 71.8
condition 14 24 58.3
subroutine 24 26 92.3
pod 14 16 87.5
total 142 167 85.0


line stmt bran cond sub pod time code
1             package RDF::Lazy::Node;
2 9     9   50 use strict;
  9         16  
  9         266  
3 9     9   46 use warnings;
  9         16  
  9         298  
4              
5 9     9   4646 use RDF::Lazy::Literal;
  9         24  
  9         250  
6 9     9   4577 use RDF::Lazy::Resource;
  9         25  
  9         241  
7 9     9   4489 use RDF::Lazy::Blank;
  9         20  
  9         262  
8 9     9   48 use RDF::Trine qw(iri);
  9         16  
  9         430  
9 9     9   41 use CGI qw(escapeHTML);
  9         41  
  9         49  
10 9     9   808 use Carp qw(carp);
  9         18  
  9         8628  
11              
12             our $AUTOLOAD;
13             our $rdf_type = iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
14              
15 288     288 1 1260 sub trine { shift->[0]; }
16 68     68 1 965 sub graph { shift->[1]; }
17 4     4 1 4260 sub esc { escapeHTML( shift->str ) }
18              
19 20     20 1 4405 sub is_literal { shift->[0]->is_literal; }
20 9     9 1 162 sub is_resource { shift->[0]->is_resource; }
21             *is_uri = *is_resource;
22              
23 4     4 1 73 sub is_blank { shift->[0]->is_blank; }
24              
25             sub AUTOLOAD {
26 29     29   49980 my $self = shift;
27 29 50 33     215 return if !ref($self) or $AUTOLOAD =~ /^(.+::)?DESTROY$/;
28              
29 29         50 my $method = $AUTOLOAD;
30 29         134 $method =~ s/.*:://;
31              
32 29         123 return $self->_autoload( $method, @_ );
33             }
34              
35             sub type {
36 9     9 1 11852 my $self = shift;
37 9 100       32 if ( @_ ) {
38 6         23 my $types = $self->rels( $rdf_type ); # TODO use filter?
39 6         16 foreach ( @_ ) {
40 8 50       42 my $type = $self->graph->uri( $_ ) or next;
41 8 100       40 return 1 if (grep { $_->str eq $type->str } @$types);
  8         22  
42             }
43 1         18 return 0;
44             } else {
45 3         17 $self->rel( $rdf_type );
46             }
47             }
48              
49             *a = *type;
50              
51             sub types {
52 2     2 0 1133 my $self = shift;
53 2         7 $self->rels( $rdf_type );
54             }
55              
56             sub is {
57 16     16 1 47 my $self = shift;
58 16 50       49 return 1 unless @_;
59              
60 16         32 foreach my $check (@_) {
61 16 100       47 if ($self->is_literal) {
    100          
    50          
62 14 50 33     174 return 1 if $check eq '' or $check eq 'literal';
63 14 100 100     86 return 1 if $check eq '@' and $self->lang;
64 13 100 100     65 return 1 if $check =~ /^@(.+)/ and $self->lang($1);
65 10 100 100     40 return 1 if $check =~ /^\^\^?$/ and $self->datatype;
66 9 50 33     44 return 1 if $check =~ /^\^\^?(.+)$/ and $self->datatype($1);
67             } elsif ($self->is_resource) {
68 1 50 33     15 return 1 if $check eq ':' or $check eq 'resource';
69             } elsif ($self->is_blank) {
70 1 50 33     15 return 1 if $check eq '-' or $check eq 'blank';
71             }
72             }
73              
74 10         43 return 0;
75             }
76              
77 0     0 1 0 sub ttl { $_[0]->graph->ttl( @_ ); }
78 1     1 0 9 sub ttlpre { $_[0]->graph->ttlpre( @_ ); }
79              
80 24     24 1 1555 sub rel { $_[0]->graph->rel( @_ ); }
81 12     12 1 1247 sub rels { $_[0]->graph->rels( @_ ); }
82 2     2 1 1074 sub rev { $_[0]->graph->rev( @_ ); }
83 5     5 1 1342 sub revs { $_[0]->graph->revs( @_ ); }
84              
85 0     0 1 0 sub qname { "" };
86              
87             sub _autoload {
88 18     18   32 my $self = shift;
89 18         31 my $property = shift;
90 18 50       77 return if $property =~ /^(query|lang)$/; # reserved words
91 18         68 return $self->rel( $property, @_ );
92             }
93              
94             1;
95             __END__
96              
97             =head1 NAME
98              
99             RDF::Lazy::Node - A node in a lazy RDF graph
100              
101             =head1 DESCRIPTION
102              
103             You should not directly create instances of this class, but use L<RDF::Lazy> as
104             node factory to create instances of L<RDF::Lazy::Resource>,
105             L<RDF::Lazy::Literal>, and L<RDF::Lazy::Blank>.
106              
107             $graph->resource( $uri ); # returns a RDF::Lazy::Resource
108             $graph->literal( $string ); # returns a RDF::Lazy::Literal
109             $graph->blank( $id ); # returns a RDF::Lazy::Blank
110              
111             A lazy node contains a L<RDF::Trine::Node> and a pointer to the
112             RDF::Lazy graph where the node is located in. You can create a
113             RDF::Lazy::Node from a RDF::Trine::Node just like this:
114              
115             $graph->uri( $trine_node )
116              
117             =head1 DESCRIPTION
118              
119             This class wraps L<RDF::Trine::Node> and holds a pointer to the graph
120             (L<RDF::Lazy>) which a node belongs to. In detail there are node types
121             L<RDF::Lazy::Literal>, L<RDF::Lazy::Resource>, and L<RDF::Lazy::Blank>.
122              
123             =head1 METHODS
124              
125             =head2 str
126              
127             Returns a string representation of the node's value. Is automatically
128             called on string conversion (C<< "$x" >> equals C<< $x->str >>).
129              
130             =head2 esc
131              
132             Returns a HTML-escaped string representation. This can safely be used
133             in HTML and XML.
134              
135             =head2 is_literal / is_resource / is_blank
136              
137             Returns true if the node is a literal, resource, or blank node.
138              
139             =head2 graph
140              
141             Returns the underlying graph L<RDF::Lazy> that the node belongs to.
142              
143             =head2 type ( [ @types ] )
144              
145             Returns some rdf:type of the node (if no types are provided) or checks
146             whether this node is of any of the provided types.
147              
148             =head2 a ( [ @types ] )
149              
150             Shortcut for C<type>.
151              
152             =head2 is ( $check1 [, $check2 ... ] )
153              
154             Checks whether the node fullfills some matching criteria, for instance
155              
156             $x->is('') # is_literal
157             $x->is(':') # is_resource
158             $x->is('-') # is_blank
159             $x->is('@') # is_literal and has language tag
160             $x->is('@en') # is_literal and has language tag 'en' (is_en)
161             $x->is('@en-') # is_literal and is_en_
162             $x->is('^') # is_literal and has datatype
163             $x->is('^^') # is_literal and has datatype
164              
165             =head2 ttl
166              
167             Returns an RDF/Turtle representation of the node's bounded connections.
168              
169             =head2 rel ( $property [, @filters ] )
170              
171             Traverse the graph and return the first matching object.
172              
173             =head2 rels
174              
175             Traverse the graph and return all matching objects.
176              
177             =head2 rev ( $property [, @filters ] )
178              
179             Traverse the graph and return the first matching subject.
180              
181             =head2 revs
182              
183             Traverse the graph and return all matching subjects.
184              
185             =head2 trine
186              
187             Returns the underlying L<RDF::Trine::Node>. DO NOT USE THIS METHOD!
188              
189             =head2 qname
190              
191             Returns a qualified string, if possible, or the empty string.
192              
193             =head1 TRAVERSING THE GRAPH
194              
195             Any other method name is used to query objects. The following three statements
196             are equivalent:
197              
198             $x->rel('foaf:name');
199             $x->graph->rel( $x, 'foaf_name' );
200             $x->rel('foaf_name');
201             $x->foaf_name;
202              
203             You can also add filters in a XPath-like language (the use of RDF::Lazy
204             in a template is an example of a "RDFPath" language):
205              
206             $x->dc_title('@en') # literal with language tag @en
207             $x->dc_title('@en-') # literal with language tag @en or @en-...
208             $x->dc_title('') # any literal
209             $x->dc_title('@') # literal with any language tag
210             $x->dc_title('^') # literal with any datatype
211             $x->foaf_knows(':') # any resource
212             ...
213              
214             =cut