File Coverage

blib/lib/Wikibase/Datatype/Value/Quantity.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 1 2 50.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Value::Quantity;
2              
3 8     8   895742 use strict;
  8         106  
  8         231  
4 8     8   44 use warnings;
  8         32  
  8         319  
5              
6 8     8   3740 use Error::Pure qw(err);
  8         82281  
  8         152  
7 8     8   4069 use Mo qw(build is);
  8         4341  
  8         45  
8 8     8   15186 use Mo::utils qw(check_number);
  8         11290  
  8         134  
9 8     8   4169 use Wikibase::Datatype::Utils qw(check_entity);
  8         30  
  8         173  
10              
11             our $VERSION = 0.29;
12              
13             extends 'Wikibase::Datatype::Value';
14              
15             has lower_bound => (
16             is => 'ro',
17             );
18              
19             has unit => (
20             is => 'ro',
21             );
22              
23             has upper_bound => (
24             is => 'ro',
25             );
26              
27             sub type {
28 1     1 1 10 return 'quantity';
29             }
30              
31             sub BUILD {
32 16     16 0 89 my $self = shift;
33              
34 16 100       57 if (defined $self->{'unit'}) {
35 3         12 check_entity($self, 'unit');
36             }
37              
38 15         56 check_number($self, 'value');
39              
40 15 100       310 if (defined $self->{'lower_bound'}) {
41 3         11 check_number($self, 'lower_bound');
42 3 100       47 if ($self->{'lower_bound'} >= $self->{'value'}) {
43 2         6 err "Parameter 'lower_bound' must be less than value.";
44             }
45             }
46 13 100       50 if (defined $self->{'upper_bound'}) {
47 3         15 check_number($self, 'upper_bound');
48 3 100       49 if ($self->{'upper_bound'} <= $self->{'value'}) {
49 2         7 err "Parameter 'upper_bound' must be greater than value.";
50             }
51             }
52              
53 11         43 return;
54             }
55              
56             1;
57              
58             __END__
59              
60             =pod
61              
62             =encoding utf8
63              
64             =head1 NAME
65              
66             Wikibase::Datatype::Value::Quantity - Wikibase quantity value datatype.
67              
68             =head1 SYNOPSIS
69              
70             use Wikibase::Datatype::Value::Quantity;
71              
72             my $obj = Wikibase::Datatype::Value::Quantity->new(%params);
73             my $lower_bound = $obj->lower_bound;
74             my $type = $obj->type;
75             my $unit = $obj->unit;
76             my $upper_bound = $obj->upper_bound;
77             my $value = $obj->value;
78              
79             =head1 DESCRIPTION
80              
81             This datatype is quantity class for representation of quantity. Optionaly we can
82             define unit of quantity.
83              
84             =head1 METHODS
85              
86             =head2 C<new>
87              
88             my $obj = Wikibase::Datatype::Value::Quantity->new(%params);
89              
90             Constructor.
91              
92             Returns instance of object.
93              
94             =over 8
95              
96             =item * C<lower_bound>
97              
98             Lower bound of value.
99             Value must be a positive or negative number.
100             Parameter is optional.
101             Default value is 0.
102              
103             =item * C<unit>
104              
105             Unit of instance.
106             Default value is 1 (without unit).
107              
108             =item * C<upper_bound>
109              
110             Upper bound of value.
111             Value must be a positive or negative number.
112             Parameter is optional.
113             Default value is 0.
114              
115             =item * C<value>
116              
117             Value of instance.
118             Value must be a positive or negative number.
119             Parameter is required.
120              
121             =back
122              
123             =head2 C<lower_bound>
124              
125             my $lower_bound = $obj->lower_bound;
126              
127             Get lower bound.
128              
129             Returns number.
130              
131             =head2 C<type>
132              
133             my $type = $obj->type;
134              
135             Get type. This is constant 'string'.
136              
137             Returns string.
138              
139             =head2 C<unit>
140              
141             my $unit = $obj->unit;
142              
143             Get unit. Unit is entity (e.g. /^Q\d+$/).
144              
145             Returns string.
146              
147             =head2 C<upper_bound>
148              
149             my $upper_bound = $obj->upper_bound;
150              
151             Get upper bound.
152              
153             Returns number.
154              
155             =head2 C<value>
156              
157             my $value = $obj->value;
158              
159             Get value.
160              
161             Returns string.
162              
163             =head1 ERRORS
164              
165             new():
166             From Mo::utils::check_number():
167             Parameter 'lower_bound' must be a number.
168             Parameter 'upper_bound' must be a number.
169             Parameter 'value' must be a number.
170             From Wikibase::Datatype::Utils::check_entity():
171             Parameter 'unit' must begin with 'Q' and number after it.
172             From Wikibase::Datatype::Value::new():
173             Parameter 'value' is required.
174             Parameter 'lower_bound' must be less than value.
175             Parameter 'upper_bound' must be greater than value.
176              
177             =head1 EXAMPLE1
178              
179             =for comment filename=create_and_print_value_quantity1.pl
180              
181             use strict;
182             use warnings;
183              
184             use Wikibase::Datatype::Value::Quantity;
185              
186             # Object.
187             my $obj = Wikibase::Datatype::Value::Quantity->new(
188             'value' => '10',
189             );
190              
191             # Get type.
192             my $type = $obj->type;
193              
194             # Get unit.
195             my $unit = $obj->unit;
196              
197             # Get value.
198             my $value = $obj->value;
199              
200             # Print out.
201             print "Type: $type\n";
202             if (defined $unit) {
203             print "Unit: $unit\n";
204             }
205             print "Value: $value\n";
206              
207             # Output:
208             # Type: quantity
209             # Value: 10
210              
211             =head1 EXAMPLE2
212              
213             =for comment filename=create_and_print_value_quantity2.pl
214              
215             use strict;
216             use warnings;
217              
218             use Wikibase::Datatype::Value::Quantity;
219              
220             # Object.
221             my $obj = Wikibase::Datatype::Value::Quantity->new(
222             'unit' => 'Q190900',
223             'value' => '10',
224             );
225              
226             # Get type.
227             my $type = $obj->type;
228              
229             # Get unit.
230             my $unit = $obj->unit;
231              
232             # Get value.
233             my $value = $obj->value;
234              
235             # Print out.
236             print "Type: $type\n";
237             print "Unit: $unit\n";
238             print "Value: $value\n";
239              
240             # Output:
241             # Type: quantity
242             # Unit: Q190900
243             # Value: 10
244              
245             =head1 DEPENDENCIES
246              
247             L<Error::Pure>,
248             L<Mo>,
249             L<Mo::utils>,
250             L<Wikibase::Datatype::Utils>,
251             L<Wikibase::Datatype::Value>.
252              
253             =head1 SEE ALSO
254              
255             =over
256              
257             =item L<Wikibase::Datatype::Value>
258              
259             Wikibase datatypes.
260              
261             =back
262              
263             =head1 REPOSITORY
264              
265             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
266              
267             =head1 AUTHOR
268              
269             Michal Josef Špaček L<mailto:skim@cpan.org>
270              
271             L<http://skim.cz>
272              
273             =head1 LICENSE AND COPYRIGHT
274              
275             © 2020-2023 Michal Josef Špaček
276              
277             BSD 2-Clause License
278              
279             =head1 VERSION
280              
281             0.29
282              
283             =cut