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   911008 use base qw(Exporter);
  35         130  
  35         3226  
4 35     35   237 use strict;
  35         76  
  35         723  
5 35     35   180 use warnings;
  35         83  
  35         1133  
6              
7 35     35   1135 use Error::Pure qw(err);
  35         23465  
  35         1448  
8 35     35   366 use Readonly;
  35         124  
  35         1530  
9 35     35   14937 use Wikibase::Datatype::Print::Value;
  35         123  
  35         9433  
10              
11             Readonly::Array our @EXPORT_OK => qw(print);
12              
13             our $VERSION = 0.13;
14              
15             sub print {
16 34     34 1 3687 my ($obj, $opts_hr) = @_;
17              
18 34 100       181 if (! $obj->isa('Wikibase::Datatype::Snak')) {
19 1         6 err "Object isn't 'Wikibase::Datatype::Snak'.";
20             }
21              
22 33         84 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         163 my $ret = $obj->property.$property_name.': ';
33 33 100       316 if ($obj->snaktype eq 'value') {
    100          
    50          
34 31         290 $ret .= Wikibase::Datatype::Print::Value::print($obj->datavalue, $opts_hr);
35             } elsif ($obj->snaktype eq 'novalue') {
36 1         16 $ret .= 'no value';
37             } elsif ($obj->snaktype eq 'somevalue') {
38 1         22 $ret .= 'unknown value';
39             } else {
40 0         0 err 'Bad snaktype.',
41             'snaktype', $obj->snaktype,
42             ;
43             }
44              
45 33         228 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             my @pretty_print_lines = print($obj, $opts_hr);
66              
67             =head1 SUBROUTINES
68              
69             =head2 C<print>
70              
71             my $pretty_print_string = print($obj, $opts_hr);
72             my @pretty_print_lines = print($obj, $opts_hr);
73              
74             Construct pretty print output for L<Wikibase::Datatype::Snak>
75             object.
76              
77             Returns string in scalar context.
78             Returns list of lines in array context.
79              
80             =head1 ERRORS
81              
82             print():
83             Object isn't 'Wikibase::Datatype::Snak'.
84             Bad snaktype.
85             snaktype: %s
86              
87             =head1 EXAMPLE1
88              
89             =for comment filename=create_and_print_snak.pl
90              
91             use strict;
92             use warnings;
93              
94             use Wikibase::Datatype::Print::Snak;
95             use Wikibase::Datatype::Snak;
96             use Wikibase::Datatype::Value::Item;
97              
98             # Object.
99             my $obj = Wikibase::Datatype::Snak->new(
100             'datatype' => 'wikibase-item',
101             'datavalue' => Wikibase::Datatype::Value::Item->new(
102             'value' => 'Q5',
103             ),
104             'property' => 'P31',
105             );
106              
107             # Print.
108             print Wikibase::Datatype::Print::Snak::print($obj)."\n";
109              
110             # Output:
111             # P31: Q5
112              
113             =head1 EXAMPLE2
114              
115             =for comment filename=create_and_print_snak_translated.pl
116              
117             use strict;
118             use warnings;
119              
120             use Wikibase::Cache;
121             use Wikibase::Cache::Backend::Basic;
122             use Wikibase::Datatype::Print::Snak;
123             use Wikibase::Datatype::Snak;
124             use Wikibase::Datatype::Value::Item;
125              
126             # Object.
127             my $obj = Wikibase::Datatype::Snak->new(
128             'datatype' => 'wikibase-item',
129             'datavalue' => Wikibase::Datatype::Value::Item->new(
130             'value' => 'Q5',
131             ),
132             'property' => 'P31',
133             );
134              
135             # Cache.
136             my $cache = Wikibase::Cache->new(
137             'backend' => 'Basic',
138             );
139              
140             # Print.
141             print Wikibase::Datatype::Print::Snak::print($obj, {
142             'cache' => $cache,
143             })."\n";
144              
145             # Output:
146             # P31 (instance of): Q5
147              
148             =head1 DEPENDENCIES
149              
150             L<Error::Pure>,
151             L<Exporter>,
152             L<Readonly>,
153             L<Wikibase::Datatype::Print::Value>.
154              
155             =head1 SEE ALSO
156              
157             =over
158              
159             =item L<Wikibase::Datatype::Snak>
160              
161             Wikibase snak datatype.
162              
163             =back
164              
165             =head1 REPOSITORY
166              
167             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
168              
169             =head1 AUTHOR
170              
171             Michal Josef Špaček L<mailto:skim@cpan.org>
172              
173             L<http://skim.cz>
174              
175             =head1 LICENSE AND COPYRIGHT
176              
177             © 2020-2023 Michal Josef Špaček
178              
179             BSD 2-Clause License
180              
181             =head1 VERSION
182              
183             0.13
184              
185             =cut
186