File Coverage

blib/lib/KiokuDB/TypeMap/Entry/Passthrough.pm
Criterion Covered Total %
statement 29 31 93.5
branch 2 2 100.0
condition n/a
subroutine 10 12 83.3
pod 0 1 0.0
total 41 46 89.1


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::TypeMap::Entry::Passthrough;
4 13     13   43181 use Moose;
  13         374339  
  13         95  
5              
6 13     13   70313 use Carp qw(croak);
  13         26  
  13         814  
7              
8 13     13   1347 use KiokuDB::TypeMap::Entry::Compiled;
  13         22  
  13         392  
9              
10 13     13   66 no warnings 'recursion';
  13         19  
  13         604  
11              
12 13     13   57 use namespace::clean -except => 'meta';
  13         14  
  13         119  
13              
14             with qw(KiokuDB::TypeMap::Entry);
15              
16             has intrinsic => (
17             isa => "Bool",
18             is => "ro",
19             default => 0,
20             );
21              
22             sub compile {
23 82     82 0 134 my ( $self, $class ) = @_;
24              
25 82 100       2745 if ( $self->intrinsic ) {
26             return KiokuDB::TypeMap::Entry::Compiled->new(
27 78     78   349 collapse_method => sub { $_[1] },
28 1     1   23 expand_method => sub { $_[1]->data }, # only called on an Entry, if the object is just an object, this won't be called
29             id_method => "generate_uuid",
30             refresh_method => sub {
31 0     0   0 croak "Refreshing Passthrough typemap entries is not supported ($class)";
32             },
33 81         3036 entry => $self,
34             class => $class,
35             );
36             } else {
37             return KiokuDB::TypeMap::Entry::Compiled->new(
38             collapse_method => sub {
39 1     1   2 my ( $collapser, @args ) = @_;
40              
41             $collapser->collapse_first_class(
42             sub {
43 1         4 my ( $collapser, %args ) = @_;
44 1         7 return $collapser->make_entry(
45             %args,
46             data => $args{object},
47             );
48             },
49 1         8 @args,
50             );
51             },
52             expand_method => sub {
53 1     1   3 my ( $linker, $entry ) = @_;
54              
55 1         31 my $obj = $entry->data;
56              
57 1         5 $linker->register_object( $entry => $obj );
58              
59 1         2 return $obj;
60             },
61             id_method => "generate_uuid",
62             refresh_method => sub {
63 0     0     croak "Refreshing Passthrough typemap entries is not supported ($class)";
64             },
65 1         45 entry => $self,
66             class => $class,
67             );
68             }
69             }
70              
71             __PACKAGE__->meta->make_immutable;
72              
73             __PACKAGE__
74              
75             __END__
76              
77             =pod
78              
79             =head1 NAME
80              
81             KiokuDB::TypeMap::Entry::Passthrough - A typemap entry of objects that will be
82             serialized by the backend.
83              
84             =head1 SYNOPSIS
85              
86             KiokuDB::TypeMap->new(
87             entires => {
88             'Value::Object' => KiokuDB::TypeMap::Entry::Naive->new,
89             },
90             );
91              
92             =head1 DESCRIPTION
93              
94             This typemap entry delegates the handling of certain objects to the backend.
95              
96             A prime example is L<DateTime> being handled by
97             L<KiokuDB::Backend::Serialize::Storable>. L<DateTime> has efficient L<Storable>
98             hooks, and does not refer to any domain objects, so it is safe to assume that
99             it can just be passed through for serialization.
100              
101             =head1 ATTRIBUTES
102              
103             =over 4
104              
105             =item intrinsic
106              
107             If true the object will be just left in place.
108              
109             If false, the object will get its own ID and entry, and the object will be in
110             the C<data> field of that entry.
111              
112             =back