File Coverage

blib/lib/Wikibase/Datatype/Print/Value/Item.pm
Criterion Covered Total %
statement 25 25 100.0
branch 6 6 100.0
condition 5 6 83.3
subroutine 6 6 100.0
pod 1 1 100.0
total 43 44 97.7


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Value::Item;
2              
3 48     48   931790 use base qw(Exporter);
  48         135  
  48         4303  
4 48     48   322 use strict;
  48         128  
  48         946  
5 48     48   256 use warnings;
  48         167  
  48         1404  
6              
7 48     48   1131 use Error::Pure qw(err);
  48         22870  
  48         2100  
8 48     48   591 use Readonly;
  48         124  
  48         11336  
9              
10             Readonly::Array our @EXPORT_OK => qw(print);
11              
12             our $VERSION = 0.09;
13              
14             sub print {
15 32     32 1 7223 my ($obj, $opts_hr) = @_;
16              
17 32 100       163 if (! $obj->isa('Wikibase::Datatype::Value::Item')) {
18 1         5 err "Object isn't 'Wikibase::Datatype::Value::Item'.";
19             }
20              
21 31 100 100     155 if (exists $opts_hr->{'cb'} && ! $opts_hr->{'cb'}->isa('Wikibase::Cache')) {
22 1         8 err "Option 'cb' must be a instance of Wikibase::Cache.";
23             }
24              
25 30         61 my $item;
26 30 100       102 if (exists $opts_hr->{'cb'}) {
27 2   66     7 $item = $opts_hr->{'cb'}->get('label', $obj->value) || $obj->value;
28             } else {
29 28         147 $item = $obj->value;
30             }
31              
32 30         420 return $item;
33             }
34              
35             1;
36              
37             __END__
38              
39             =pod
40              
41             =encoding utf8
42              
43             =head1 NAME
44              
45             Wikibase::Datatype::Print::Value::Item - Wikibase item value pretty print helpers.
46              
47             =head1 SYNOPSIS
48              
49             use Wikibase::Datatype::Print::Value::Item qw(print);
50              
51             my $pretty_print_string = print($obj, $opts_hr);
52              
53             =head1 SUBROUTINES
54              
55             =head2 C<print>
56              
57             my $pretty_print_string = print($obj, $opts_hr);
58              
59             Construct pretty print output for L<Wikibase::Datatype::Value::Item>
60             object.
61              
62             Returns string.
63              
64             =head1 ERRORS
65              
66             print():
67             Object isn't 'Wikibase::Datatype::Value::Item'.
68             Option 'cb' must be a instance of Wikibase::Cache.
69              
70             =head1 EXAMPLE
71              
72             =for comment filename=create_and_print_value_item.pl
73              
74             use strict;
75             use warnings;
76              
77             use Wikibase::Datatype::Print::Value::Item;
78             use Wikibase::Datatype::Value::Item;
79              
80             # Object.
81             my $obj = Wikibase::Datatype::Value::Item->new(
82             'value' => 'Q123',
83             );
84              
85             # Print.
86             print Wikibase::Datatype::Print::Value::Item::print($obj)."\n";
87              
88             # Output:
89             # Q123
90              
91             =head1 DEPENDENCIES
92              
93             L<Error::Pure>,
94             L<Exporter>,
95             L<Readonly>.
96              
97             =head1 SEE ALSO
98              
99             =over
100              
101             =item L<Wikibase::Datatype::Value::Item>
102              
103             Wikibase item value datatype.
104              
105             =back
106              
107             =head1 REPOSITORY
108              
109             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
110              
111             =head1 AUTHOR
112              
113             Michal Josef Špaček L<mailto:skim@cpan.org>
114              
115             L<http://skim.cz>
116              
117             =head1 LICENSE AND COPYRIGHT
118              
119             © 2020-2023 Michal Josef Špaček
120              
121             BSD 2-Clause License
122              
123             =head1 VERSION
124              
125             0.09
126              
127             =cut