File Coverage

blib/lib/Data/Keys/E/Key/Adapt.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Data::Keys::E::Key::Adapt;
2              
3             =head1 NAME
4              
5             Data::Keys::E::Key::Adapt - change key with a callback
6              
7             =head1 DESCRIPTION
8              
9             Uses callback to change keys.
10              
11             =cut
12              
13 2     2   908 use warnings;
  2         2  
  2         52  
14 2     2   7 use strict;
  2         2  
  2         52  
15              
16             our $VERSION = '0.03';
17              
18 2     2   6 use Moose::Role;
  2         2  
  2         9  
19              
20             requires('get', 'set');
21              
22             =head1 PROPERTIES
23              
24             =head2 key_adapt
25              
26             A callback that is called with a key as an argument for every set and get.
27             The default callback will not change the key.
28              
29             =cut
30              
31             has 'key_adapt' => ( isa => 'CodeRef', is => 'rw', lazy => 1, default => sub { sub { return $_[0] } } );
32              
33             around 'get' => sub {
34             my $get = shift;
35             my $self = shift;
36             my $key = shift;
37              
38             $key = $self->key_adapt->($key);
39             return $self->$get($key);
40             };
41              
42             around 'set' => sub {
43             my $set = shift;
44             my $self = shift;
45             my $key = shift;
46             my $value = shift;
47            
48             $key = $self->key_adapt->($key, $value);
49             $self->$set($key, $value);
50             return $key;
51             };
52              
53             1;
54              
55              
56             __END__
57              
58             =head1 AUTHOR
59              
60             Jozef Kutej
61              
62             =cut