File Coverage

blib/lib/KiokuDB/Meta/Attribute/Lazy.pm
Criterion Covered Total %
statement 9 10 90.0
branch n/a
condition n/a
subroutine 3 4 75.0
pod 0 1 0.0
total 12 15 80.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::Meta::Attribute::Lazy;
4 3     3   63 use Moose::Role;
  3         4  
  3         17  
5              
6 3     3   12340 use Moose::Util qw(does_role);
  3         4  
  3         22  
7              
8 3     3   547 use namespace::clean -except => 'meta';
  3         4  
  3         31  
9              
10 0     0 0   sub Moose::Meta::Attribute::Custom::Trait::KiokuDB::Lazy::register_implementation { __PACKAGE__ }
11              
12             before attach_to_class => sub {
13             my ( $self, $class ) = @_;
14              
15             my $mi = $class->get_meta_instance;
16              
17             unless ( does_role( $mi, "KiokuDB::Meta::Instance" ) ) {
18             $self->throw_error("Can't attach to a class whose meta instance doesn't do KiokuDB::Meta::Instance", data => $class );
19             }
20             };
21              
22             __PACKAGE__
23              
24             __END__
25              
26             =pod
27              
28             =head1 NAME
29              
30             KiokuDB::Meta::Attribute::Lazy - Trait for lazy loaded attributes
31              
32             =head1 SYNOPSIS
33              
34             # in your class:
35              
36             package Foo;
37             use KiokuDB::Class;
38              
39             has bar => (
40             traits => [qw(KiokuDB::Lazy)],
41             isa => "Bar",
42             is => "ro",
43             );
44              
45              
46              
47             # Later:
48              
49             my $foo = $dir->lookup($id);
50              
51             # bar is not yet loaded, it will be lazily fetched during this call:
52             $foo->bar;
53              
54             =head1 DESCRIPTION
55              
56             This L<Moose::Meta::Attribute> trait provides lazy loading on a per field basis
57             for objects stored in L<KiokuDB>.
58              
59             Instead of using proxy objects with AUTOLOAD, overloading, or similar hacks,
60             you can declaratively specify which attributes you want to make lazy, and this
61             will be done cleanly through the MOP.
62              
63             This is implemented by using a placeholder object, L<KiokuDB::Thunk> which
64             contains references to the ID and the linker, and L<KiokuDB::Meta::Instance>
65             will know to replace the placeholder with the actual loaded object when it is
66             fetched from the object by an accessor.
67              
68             =cut
69              
70