File Coverage

blib/lib/Wikibase/Datatype/Print/Snak.pm
Criterion Covered Total %
statement 29 34 85.2
branch 8 12 66.6
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 45 54 83.3


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Snak;
2              
3 35     35   929992 use base qw(Exporter);
  35         158  
  35         3217  
4 35     35   267 use strict;
  35         72  
  35         774  
5 35     35   160 use warnings;
  35         90  
  35         1116  
6              
7 35     35   1144 use Error::Pure qw(err);
  35         23217  
  35         1424  
8 35     35   372 use Readonly;
  35         126  
  35         1568  
9 35     35   15546 use Wikibase::Datatype::Print::Value;
  35         129  
  35         9541  
10              
11             Readonly::Array our @EXPORT_OK => qw(print);
12              
13             our $VERSION = 0.09;
14              
15             sub print {
16 34     34 1 3399 my ($obj, $opts_hr) = @_;
17              
18 34 100       182 if (! $obj->isa('Wikibase::Datatype::Snak')) {
19 1         6 err "Object isn't 'Wikibase::Datatype::Snak'.";
20             }
21              
22 33         83 my $property_name = '';
23 33 50       101 if (exists $opts_hr->{'cache'}) {
24 0         0 $property_name = $opts_hr->{'cache'}->get('label', $obj->property);
25 0 0       0 if (defined $property_name) {
26 0         0 $property_name = " ($property_name)";
27             } else {
28 0         0 $property_name = '';
29             }
30             }
31              
32 33         130 my $ret = $obj->property.$property_name.': ';
33 33 100       353 if ($obj->snaktype eq 'value') {
    100          
    50          
34 31         289 $ret .= Wikibase::Datatype::Print::Value::print($obj->datavalue, $opts_hr);
35             } elsif ($obj->snaktype eq 'novalue') {
36 1         15 $ret .= 'no value';
37             } elsif ($obj->snaktype eq 'somevalue') {
38 1         32 $ret .= 'unknown value';
39             } else {
40 0         0 err 'Bad snaktype.',
41             'snaktype', $obj->snaktype,
42             ;
43             }
44              
45 33         155 return $ret;
46             }
47              
48             1;
49              
50             __END__
51              
52             =pod
53              
54             =encoding utf8
55              
56             =head1 NAME
57              
58             Wikibase::Datatype::Print::Snak - Wikibase snak pretty print helpers.
59              
60             =head1 SYNOPSIS
61              
62             use Wikibase::Datatype::Print::Snak qw(print);
63              
64             my $pretty_print_string = print($obj, $opts_hr);
65              
66             =head1 SUBROUTINES
67              
68             =head2 C<print>
69              
70             my $pretty_print_string = print($obj, $opts_hr);
71              
72             Construct pretty print output for L<Wikibase::Datatype::Snak>
73             object.
74              
75             Returns string.
76              
77             =head1 ERRORS
78              
79             print():
80             Object isn't 'Wikibase::Datatype::Snak'.
81             Bad snaktype.
82             snaktype: %s
83              
84             =head1 EXAMPLE1
85              
86             =for comment filename=create_and_print_snak.pl
87              
88             use strict;
89             use warnings;
90              
91             use Wikibase::Datatype::Print::Snak;
92             use Wikibase::Datatype::Snak;
93             use Wikibase::Datatype::Value::Item;
94              
95             # Object.
96             my $obj = Wikibase::Datatype::Snak->new(
97             'datatype' => 'wikibase-item',
98             'datavalue' => Wikibase::Datatype::Value::Item->new(
99             'value' => 'Q5',
100             ),
101             'property' => 'P31',
102             );
103              
104             # Print.
105             print Wikibase::Datatype::Print::Snak::print($obj)."\n";
106              
107             # Output:
108             # P31: Q5
109              
110             =head1 EXAMPLE2
111              
112             =for comment filename=create_and_print_snak_translated.pl
113              
114             use strict;
115             use warnings;
116              
117             use Wikibase::Cache;
118             use Wikibase::Cache::Backend::Basic;
119             use Wikibase::Datatype::Print::Snak;
120             use Wikibase::Datatype::Snak;
121             use Wikibase::Datatype::Value::Item;
122              
123             # Object.
124             my $obj = Wikibase::Datatype::Snak->new(
125             'datatype' => 'wikibase-item',
126             'datavalue' => Wikibase::Datatype::Value::Item->new(
127             'value' => 'Q5',
128             ),
129             'property' => 'P31',
130             );
131              
132             # Cache.
133             my $cache = Wikibase::Cache->new(
134             'backend' => 'Basic',
135             );
136              
137             # Print.
138             print Wikibase::Datatype::Print::Snak::print($obj, {
139             'cache' => $cache,
140             })."\n";
141              
142             # Output:
143             # P31 (instance of): Q5
144              
145             =head1 DEPENDENCIES
146              
147             L<Error::Pure>,
148             L<Exporter>,
149             L<Readonly>,
150             L<Wikibase::Datatype::Print::Value>.
151              
152             =head1 SEE ALSO
153              
154             =over
155              
156             =item L<Wikibase::Datatype::Snak>
157              
158             Wikibase snak datatype.
159              
160             =back
161              
162             =head1 REPOSITORY
163              
164             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
165              
166             =head1 AUTHOR
167              
168             Michal Josef Špaček L<mailto:skim@cpan.org>
169              
170             L<http://skim.cz>
171              
172             =head1 LICENSE AND COPYRIGHT
173              
174             © 2020-2023 Michal Josef Špaček
175              
176             BSD 2-Clause License
177              
178             =head1 VERSION
179              
180             0.09
181              
182             =cut
183