File Coverage

blib/lib/Data/Quantity/Abstract/InstanceIndex.pm
Criterion Covered Total %
statement 25 29 86.2
branch 3 8 37.5
condition 3 8 37.5
subroutine 7 8 87.5
pod 0 4 0.0
total 38 57 66.6


line stmt bran cond sub pod time code
1             ### Change History
2             # 1998-12-03 Created. -Simon
3              
4             package Data::Quantity::Abstract::InstanceIndex;
5              
6             require 5;
7 3     3   26 use strict;
  3         4  
  3         159  
8              
9 3     3   16 use vars qw( $VERSION );
  3         5  
  3         144  
10             $VERSION = 0.001;
11              
12 3     3   2030 use Data::Quantity::Number::Integer '-isasubclass';
  3         9  
  3         828  
13              
14             use Class::MakeMethods (
15 3         26 'Standard::Inheritable:scalar' => 'instances',
16 3     3   4332 );
  3         10052  
17              
18             # $flag = Data::Quantity::Abstract::InstanceIndex->is_valid_index($number);
19             # Is this value acceptable for this quantity.
20             sub is_valid_index {
21 0     0 0 0 my $class_or_item = shift;
22 0         0 my $value = shift;
23            
24 0 0       0 my $instances = $class_or_item->instances or return;
25 0   0     0 return ( $value >= 0 and $value == int($value) and $value <= $#$instances );
26             }
27              
28             # $flag = Data::Quantity::Abstract::InstanceIndex->is_index_value($number);
29             # Is this value acceptable, and *not blank or empty*, for this quantity.
30             sub is_index_value {
31 11     11 0 16 my $class_or_item = shift;
32 11         15 my $value = shift;
33            
34 11 50       48 my $instances = $class_or_item->instances or return;
35 11   33     367 return ( $value > 0 and $value == int($value) and $value <= $#$instances );
36             }
37              
38             # $definition = Data::Quantity::Abstract::InstanceIndex->instance($number);
39             sub instance {
40 11     11 0 15 my $class_or_item = shift;
41 11         15 my $value = shift;
42            
43 11 50       38 return unless $class_or_item->is_index_value($value);
44 11         38 return $class_or_item->instances->[ $value ];
45             }
46              
47             # $value = Data::Quantity::Abstract::InstanceIndex->readable_value($number)
48             # $value = Data::Quantity::Abstract::InstanceIndex->readable_value($number, $style)
49             sub readable_value {
50 11     11 0 15 my $class_or_item = shift;
51 11         17 my $value = shift;
52 11   100     42 my $style = shift || 'name';
53            
54 11 50       39 my $instance = $class_or_item->instance($value) or return;
55            
56 11         267 return $instance->{ $style };
57             }
58              
59             1;