File Coverage

blib/lib/HashDataRole/Source/Hash.pm
Criterion Covered Total %
statement 43 52 82.6
branch 12 20 60.0
condition n/a
subroutine 12 14 85.7
pod 0 11 0.0
total 67 97 69.0


line stmt bran cond sub pod time code
1             package HashDataRole::Source::Hash;
2              
3 4     4   341843 use 5.010001;
  4         16  
4 4     4   648 use Role::Tiny;
  4         8764  
  4         36  
5 4     4   1813 use Role::Tiny::With;
  4         376  
  4         3814  
6             with 'HashDataRole::Spec::Basic';
7             with 'Role::TinyCommons::Collection::GetItemByPos'; # bonus
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2024-11-04'; # DATE
11             our $DIST = 'HashDataRoles-Standard'; # DIST
12             our $VERSION = '0.005'; # VERSION
13              
14             sub new {
15 1     1 0 365851 my ($class, %args) = @_;
16              
17 1 50       4 my $hash = delete $args{hash} or die "Please specify 'hash' argument";
18              
19 1 50       3 die "Unknown argument(s): ". join(", ", sort keys %args)
20             if keys %args;
21              
22             # cache keys for iteration
23 1         8 my $keys = [];
24 1         5 for my $key (sort keys %$hash) {
25 3         5 push @$keys, $key;
26             }
27              
28             bless {
29 1         6 hash => $hash,
30             _keys => $keys,
31             pos => 0,
32             }, $class;
33             }
34              
35             sub get_next_item {
36 5     5 0 99 my $self = shift;
37 5 100       12 die "StopIteration" unless $self->{pos} < @{ $self->{_keys} };
  5         32  
38 4         13 my $key = $self->{_keys}->[ $self->{pos}++ ];
39 4         32 [$key, $self->{hash}{$key}];
40             }
41              
42             sub has_next_item {
43 2     2 0 6 my $self = shift;
44 2         10 $self->{pos} < @{ $self->{_keys} };
  2         21  
45             }
46              
47             sub reset_iterator {
48 2     2 0 7 my $self = shift;
49 2         29 $self->{pos} = 0;
50             }
51              
52             sub get_iterator_pos {
53 0     0 0 0 my $self = shift;
54 0         0 $self->{pos};
55             }
56              
57             sub get_item_count {
58 1     1 0 3 my $self = shift;
59 1         3 scalar @{ $self->{_keys} };
  1         8  
60             }
61              
62             sub get_item_at_pos {
63 2     2 0 46 my ($self, $pos) = @_;
64 2 50       8 if ($pos < 0) {
65 0 0       0 die "Out of range" unless -$pos <= @{ $self->{_keys} };
  0         0  
66             } else {
67 2 100       4 die "Out of range" unless $pos < @{ $self->{_keys} };
  2         27  
68             }
69 1         4 my $key = $self->{_keys}->[$pos];
70 1         8 [$key, $self->{hash}{$key}];
71             }
72              
73             sub has_item_at_pos {
74 2     2 0 7 my ($self, $pos) = @_;
75 2 50       9 if ($pos < 0) {
76 0 0       0 return -$pos <= @{ $self->{_keys} } ? 1:0;
  0         0  
77             } else {
78 2 100       4 return $pos < @{ $self->{_keys} } ? 1:0;
  2         16  
79             }
80             }
81              
82             sub get_item_at_key {
83 2     2 0 45 my ($self, $key) = @_;
84 2 100       21 die "No such key '$key'" unless exists $self->{hash}{$key};
85 1         6 $self->{hash}{$key};
86             }
87              
88             sub has_item_at_key {
89 2     2 0 8 my ($self, $key) = @_;
90 2         13 exists $self->{hash}{$key};
91             }
92              
93             sub get_all_keys {
94 0     0 0   my ($self, $key) = @_;
95 0           @{$self->{_keys}};
  0            
96             }
97              
98             1;
99             # ABSTRACT: Get hash data from a Perl hash
100              
101             __END__