File Coverage

blib/lib/Data/Keys/E/Key/Auto.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Data::Keys::E::Key::Auto;
2              
3             =head1 NAME
4              
5             Data::Keys::E::Key::Auto - auto create key via callback
6              
7             =head1 DESCRIPTION
8              
9             For set and when the key is not present C<auto_key> callback is executed
10             with a value as an argument.
11              
12             =cut
13              
14 1     1   1223 use warnings;
  1         2  
  1         33  
15 1     1   3 use strict;
  1         1  
  1         31  
16              
17             our $VERSION = '0.03';
18              
19 1     1   408 use Moose::Role;
  1         3175  
  1         3  
20 1     1   4018 use Digest::SHA1 'sha1_hex';
  1         557  
  1         132  
21              
22             =head1 PROPERTIES
23              
24             =head1 auto_key
25              
26             A callback that is executed when set is called with undef key. Default
27             callback is C<sha1_hex>.
28              
29             =cut
30              
31             has 'auto_key' => ( isa => 'CodeRef', is => 'rw', lazy => 1, default => sub { \&sha1_hex } );
32              
33             requires('set');
34              
35             around 'set' => sub {
36             my $set = shift;
37             my $self = shift;
38             my $key = shift;
39             my $value = shift;
40              
41             $key = $self->auto_key->($value)
42             if not defined $key;
43             return $self->$set($key, $value);
44             };
45              
46             1;
47              
48              
49             __END__
50              
51             =head1 AUTHOR
52              
53             Jozef Kutej
54              
55             =cut