1 package MooseX::Storage::Traits::OnlyWhenBuilt;
5 our $AUTHORITY = 'cpan:STEVAN';
10 around 'pack' => sub {
11 my ($orig, $self, %args) = @_;
12 $args{engine_traits} ||= [];
13 push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
17 around 'unpack' => sub {
18 my ($orig, $self, $data, %args) = @_;
19 $args{engine_traits} ||= [];
20 push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
21 $self->$orig($data, %args);
34 MooseX::Storage::Traits::OnlyWhenBuilt - A custom trait to bypass serialization
43 with Storage( traits => [qw|OnlyWhenBuilt|] );
45 has 'x' => (is => 'rw', lazy_build => 1 );
46 has 'y' => (is => 'rw', lazy_build => 1 );
47 has 'z' => (is => 'rw', builder => '_build_z' );
50 sub _build_y { expensive_computation() }
55 my $p = Point->new( 'x' => 4 );
57 # the result of ->pack will contain:
63 Sometimes you don't want a particular attribute to be part of the
64 serialization if it has not been built yet. If you invoke C<Storage()>
65 as outlined in the C<Synopsis>, only attributes that have been built
66 (ie, where the predicate returns 'true') will be serialized.
67 This avoids any potentially expensive computations.
69 See the SYNOPSIS for a nice example that can be easily cargo-culted.
83 All complex software has bugs lurking in it, and this module is no
84 exception. If you find a bug please either email me, or add the bug
89 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
91 =head1 COPYRIGHT AND LICENSE
93 Copyright 2007-2008 by Infinity Interactive, Inc.
95 L<http://www.iinteractive.com>
97 This library is free software; you can redistribute it and/or modify
98 it under the same terms as Perl itself.