File Coverage

blib/lib/MooseX/Storage/Traits/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::Traits::OnlyWhenBuilt;
2             # ABSTRACT: A custom trait to bypass serialization
3              
4             our $VERSION = '0.53';
5              
6 1     1   593 use Moose::Role;
  1         2  
  1         8  
7 1     1   5008 use namespace::autoclean;
  1         3  
  1         9  
8              
9             requires 'pack';
10             requires 'unpack';
11              
12             around 'pack' => sub {
13             my ($orig, $self, %args) = @_;
14             $args{engine_traits} ||= [];
15             push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
16             $self->$orig(%args);
17             };
18              
19             around 'unpack' => sub {
20             my ($orig, $self, $data, %args) = @_;
21             $args{engine_traits} ||= [];
22             push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
23             $self->$orig($data, %args);
24             };
25              
26             1;
27              
28             __END__
29              
30             =pod
31              
32             =encoding UTF-8
33              
34             =head1 NAME
35              
36             MooseX::Storage::Traits::OnlyWhenBuilt - A custom trait to bypass serialization
37              
38             =head1 VERSION
39              
40             version 0.53
41              
42             =head1 SYNOPSIS
43              
44             { package Point;
45             use Moose;
46             use MooseX::Storage;
47              
48             with Storage( traits => [qw|OnlyWhenBuilt|] );
49              
50             has 'x' => (is => 'rw', lazy_build => 1 );
51             has 'y' => (is => 'rw', predicate => '_has_y' );
52             has 'z' => (is => 'rw', builder => '_build_z' );
53              
54             sub _build_x { 3 }
55             sub _build_y { expensive_computation() }
56             sub _build_z { 3 }
57              
58             }
59              
60             my $p = Point->new( 'x' => 4 );
61              
62             # the result of ->pack will contain:
63             # { x => 4, z => 3 }
64             $p->pack;
65              
66             =head1 DESCRIPTION
67              
68             Sometimes you don't want a particular attribute to be part of the
69             serialization if it has not been built yet. If you invoke C<Storage()>
70             as outlined in the C<Synopsis>, only attributes that have been built
71             (i.e., where the predicate returns 'true') will be serialized.
72             This avoids any potentially expensive computations.
73              
74             =for stopwords culted
75              
76             See the SYNOPSIS for a nice example that can be easily cargo-culted.
77              
78             =head1 SUPPORT
79              
80             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Storage>
81             (or L<bug-MooseX-Storage@rt.cpan.org|mailto:bug-MooseX-Storage@rt.cpan.org>).
82              
83             There is also a mailing list available for users of this distribution, at
84             L<http://lists.perl.org/list/moose.html>.
85              
86             There is also an irc channel available for users of this distribution, at
87             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
88              
89             =head1 AUTHORS
90              
91             =over 4
92              
93             =item *
94              
95             Chris Prather <chris.prather@iinteractive.com>
96              
97             =item *
98              
99             Stevan Little <stevan.little@iinteractive.com>
100              
101             =item *
102              
103             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
104              
105             =back
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is copyright (c) 2007 by Infinity Interactive, Inc.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =cut