File Coverage

blib/lib/Starch/Store/Memory.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Starch::Store::Memory;
2             our $VERSION = '0.14';
3              
4             =encoding utf8
5              
6             =head1 NAME
7              
8             Starch::Store::Memory - In-memory Starch store.
9              
10             =head1 DESCRIPTION
11              
12             This store provides an in-memory store using a hash ref to store the
13             data. This store is mostly here as a proof of concept and for writing
14             tests against.
15              
16             =cut
17              
18 13     13   6380 use Types::Common::Numeric -types;
  13         31  
  13         176  
19 13     13   17484 use Types::Standard -types;
  13         30  
  13         103  
20              
21 13     13   55446 use Moo;
  13         32  
  13         98  
22 13     13   5444 use strictures 2;
  13         99  
  13         540  
23 13     13   2507 use namespace::clean;
  13         29  
  13         97  
24              
25             with 'Starch::Store';
26              
27             =head1 OPTIONAL ARGUMENTS
28              
29             =head2 global
30              
31             Set this to a true value to use a shared memory store for all instances
32             of this class that enable this argument.
33              
34             =cut
35              
36             my $global_memory = {};
37              
38             has global => (
39             is => 'ro',
40             isa => Bool,
41             );
42              
43             =head2 memory
44              
45             This is the hash ref which is used for storing states.
46             Defaults to a global hash ref if L is set, or
47             a new hash ref if not.
48              
49             =cut
50              
51             has memory => (
52             is => 'lazy',
53             isa => HashRef,
54             );
55             sub _build_memory {
56 33     33   408 my ($self) = @_;
57 33 100       217 return $global_memory if $self->global();
58 29         465 return {};
59             }
60              
61             =head1 METHODS
62              
63             =head2 set
64              
65             Set L.
66              
67             =head2 get
68              
69             Set L.
70              
71             =head2 remove
72              
73             Set L.
74              
75             =cut
76              
77             sub set {
78             my ($self, $id, $namespace, $data) = @_;
79              
80             $self->memory->{
81             $self->stringify_key( $id, $namespace )
82             } = $data;
83              
84             return;
85             }
86              
87             sub get {
88 160     160 1 1528 my ($self, $id, $namespace) = @_;
89             return $self->memory->{
90 160         2698 $self->stringify_key( $id, $namespace )
91             };
92             }
93              
94             sub remove {
95 53     53 1 754 my ($self, $id, $namespace) = @_;
96             delete( $self->memory->{
97 53         914 $self->stringify_key( $id, $namespace )
98             } );
99 53         153 return;
100             }
101              
102             1;
103             __END__