File Coverage

blib/lib/PICA/Modification/Queue/Hash.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package PICA::Modification::Queue::Hash;
2             {
3             $PICA::Modification::Queue::Hash::VERSION = '0.16';
4             }
5             #ABSTRACT: In-memory collection of modifications
6              
7 1     1   2933 use strict;
  1         3  
  1         47  
8 1     1   5 use warnings;
  1         2  
  1         29  
9 1     1   14 use v5.10;
  1         3  
  1         53  
10              
11 1     1   53 use PICA::Modification::Request;
  0            
  0            
12             use Scalar::Util qw(blessed);
13              
14             sub new {
15             bless [{},0], shift;
16             }
17              
18             sub get {
19             my ($self, $id) = @_;
20             return $self->[0]->{ $id };
21             }
22              
23             sub request {
24             my ($self, $mod) = @_;
25             return if !$mod->isa('PICA::Modification') or $mod->isa('PICA::Modification::Request');
26             $mod = PICA::Modification::Request->new( $mod );
27             return if $mod->error;
28             my $id = ++$self->[1];
29             $self->[0]->{ $id } = $mod;
30             return $id;
31             }
32              
33             sub update {
34             my ($self, $id => $mod) = @_;
35             $mod = PICA::Modification::Request->new( $mod );
36             return if $mod->error;
37             $self->[0]->{ $id } = $mod;
38             return $id;
39             }
40              
41             sub delete {
42             my ($self, $id) = @_;
43             return unless defined $self->[0]->{ $id };
44             delete $self->[0]->{ $id };
45             $id;
46             }
47              
48             sub list {
49             my ($self, %properties) = @_;
50              
51             my $limit = delete $properties{limit} || 20;
52             my $page = delete $properties{page} || 1;
53             my $sortby = delete $properties{sort};
54              
55             my $hash = $self->[0];
56              
57             my $c = 0;
58             my $grep = sub {
59             while (my ($k,$v) = each(%properties)) {
60             return 0 unless $_[0]->{$k} eq $v;
61             }
62             $c++;
63             return 0 if $c > $page*$limit;
64             return 0 if $c <= ($page-1)*$limit;
65             1;
66             };
67              
68             if ( $sortby ) {
69             my $sort = sub { $a->{$sortby} cmp $b->{$sortby} };
70             return [ grep { $grep->($_) } sort $sort values %$hash ];
71             } else {
72             return [ grep { $grep->($_) } values %$hash ];
73             }
74             }
75              
76             1;
77              
78              
79             __END__