File Coverage

blib/lib/Data/Model/Schema/Inflate.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 4 50.0
condition n/a
subroutine 8 8 100.0
pod 0 3 0.0
total 36 41 87.8


line stmt bran cond sub pod time code
1             package Data::Model::Schema::Inflate;
2 74     74   22535 use strict;
  74         141  
  74         2538  
3 74     74   415 use warnings;
  74         185  
  74         1915  
4              
5 74     74   436 use Carp ();
  74         150  
  74         4102  
6             $Carp::Internal{(__PACKAGE__)}++;
7              
8             sub import {
9 82     82   268 my $class = shift;
10 82         297 my $caller = caller;
11              
12 74     74   510 no strict 'refs';
  74         194  
  74         27828  
13 82         282 *{"$caller\::inflate_type"} = \&inflate_type;
  82         7445  
14             }
15              
16             my %INFLATE = (
17             inflate => {
18             URI => sub { URI->new($_[0]) },
19             Hex => sub { unpack("H*", $_[0]) },
20             },
21             deflate => {
22             URI => sub { $_[0]->as_string },
23             Hex => sub { pack("H*", $_[0]) },
24             },
25             );
26              
27             sub get_inflate {
28 24     24 0 49 my($class, $name) = @_;
29 24         109 $INFLATE{inflate}->{$name};
30             }
31              
32             sub get_deflate {
33 24     24 0 53 my($class, $name) = @_;
34 24         100 $INFLATE{deflate}->{$name};
35             }
36              
37             sub inflate_type {
38 8     8 0 92 my($name, $hash) = @_;
39 8         23 my $caller = caller;
40 8         25 for my $type (qw/ inflate deflate /) {
41 16 50       98 if (ref($hash->{$type}) eq 'CODE') {
42 16 50       54 Carp::croak "The inflate_type '$name' has already been created, cannot be created again in $caller"
43             if $INFLATE{$type}->{$name};
44 16         61 $INFLATE{$type}->{$name} = $hash->{$type};
45             }
46             }
47             }
48              
49             1;