File Coverage

blib/lib/MooseX/Storage/Engine/Trait/OnlyWhenBuilt.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package MooseX::Storage::Engine::Trait::OnlyWhenBuilt;
2             # ABSTRACT: An engine trait to bypass serialization
3              
4             our $VERSION = '0.50';
5              
6 1     1   3299 use Moose::Role;
  1         2  
  1         10  
7 1     1   4819 use namespace::autoclean;
  1         3  
  1         9  
8              
9             # we should
10             # only serialize the attribute if it's already built. So, go ahead
11             # and check if the attribute has a predicate. If so, check if it's
12             # set and then go ahead and look it up.
13             around 'collapse_attribute' => sub {
14             my ($orig, $self, $attr, @args) = @_;
15              
16             my $pred = $attr->predicate if $attr->has_predicate;
17             if ($pred) {
18             return () unless $self->object->$pred();
19             }
20              
21             return $self->$orig($attr, @args);
22             };
23              
24             1;
25              
26             __END__
27              
28             =pod
29              
30             =encoding UTF-8
31              
32             =head1 NAME
33              
34             MooseX::Storage::Engine::Trait::OnlyWhenBuilt - An engine trait to bypass serialization
35              
36             =head1 VERSION
37              
38             version 0.50
39              
40             =head1 SYNOPSIS
41              
42             { package Point;
43             use Moose;
44             use MooseX::Storage;
45              
46             with Storage( traits => [qw|OnlyWhenBuilt|] );
47              
48             has 'x' => (is => 'rw', lazy_build => 1 );
49             has 'y' => (is => 'rw', lazy_build => 1 );
50             has 'z' => (is => 'rw', builder => '_build_z' );
51              
52             sub _build_x { 3 }
53             sub _build_y { expensive_computation() }
54             sub _build_z { 3 }
55             }
56              
57             my $p = Point->new( 'x' => 4 );
58              
59             # the result of ->pack will contain:
60             # { x => 4, z => 3 }
61             $p->pack;
62              
63             =head1 DESCRIPTION
64              
65             Sometimes you don't want a particular attribute to be part of the
66             serialization if it has not been built yet. If you invoke C<Storage()>
67             as outlined in the C<Synopsis>, only attributes that have been built
68             (i.e., where the predicate returns 'true') will be serialized.
69             This avoids any potentially expensive computations.
70              
71             This trait is applied to an instance of L<MooseX::Storage::Engine>, for the
72             user-visible version shown in the SYNOPSIS, see L<MooseX::Storage::Traits::OnlyWhenBuilt>
73              
74             =head1 METHODS
75              
76             =head2 Introspection
77              
78             =over 4
79              
80             =item B<meta>
81              
82             =back
83              
84             =head1 BUGS
85              
86             All complex software has bugs lurking in it, and this module is no
87             exception. If you find a bug please either email me, or add the bug
88             to cpan-RT.
89              
90             =head1 AUTHORS
91              
92             =over 4
93              
94             =item *
95              
96             Chris Prather <chris.prather@iinteractive.com>
97              
98             =item *
99              
100             Stevan Little <stevan.little@iinteractive.com>
101              
102             =item *
103              
104             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
105              
106             =back
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2007 by Infinity Interactive, Inc..
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut