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.53';
5              
6 1     1   3168 use Moose::Role;
  1         2  
  1         7  
7 1     1   4673 use namespace::autoclean;
  1         2  
  1         7  
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.53
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', predicate => '_has_y' );
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 SUPPORT
75              
76             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Storage>
77             (or L<bug-MooseX-Storage@rt.cpan.org|mailto:bug-MooseX-Storage@rt.cpan.org>).
78              
79             There is also a mailing list available for users of this distribution, at
80             L<http://lists.perl.org/list/moose.html>.
81              
82             There is also an irc channel available for users of this distribution, at
83             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
84              
85             =head1 AUTHORS
86              
87             =over 4
88              
89             =item *
90              
91             Chris Prather <chris.prather@iinteractive.com>
92              
93             =item *
94              
95             Stevan Little <stevan.little@iinteractive.com>
96              
97             =item *
98              
99             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
100              
101             =back
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             This software is copyright (c) 2007 by Infinity Interactive, Inc.
106              
107             This is free software; you can redistribute it and/or modify it under
108             the same terms as the Perl 5 programming language system itself.
109              
110             =cut