File Coverage

blib/lib/Wikibase/Datatype/Print/Value/Quantity.pm
Criterion Covered Total %
statement 33 33 100.0
branch 14 14 100.0
condition 7 9 77.7
subroutine 6 6 100.0
pod 1 1 100.0
total 61 63 96.8


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