1 package MooseX::Storage::Traits::OnlyWhenBuilt;
8 my ($orig, $self, %args) = @_;
9 $args{engine_traits} ||= [];
10 push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
14 around 'unpack' => sub {
15 my ($orig, $self, $data, %args) = @_;
16 $args{engine_traits} ||= [];
17 push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
18 $self->$orig($data, %args);
31 MooseX::Storage::Traits::OnlyWhenBuilt - A custom trait to bypass serialization
40 with Storage( traits => [qw|OnlyWhenBuilt|] );
42 has 'x' => (is => 'rw', lazy_build => 1 );
43 has 'y' => (is => 'rw', lazy_build => 1 );
44 has 'z' => (is => 'rw', builder => '_build_z' );
47 sub _build_y { expensive_computation() }
52 my $p = Point->new( 'x' => 4 );
54 # the result of ->pack will contain:
60 Sometimes you don't want a particular attribute to be part of the
61 serialization if it has not been built yet. If you invoke C<Storage()>
62 as outlined in the C<Synopsis>, only attributes that have been built
63 (ie, where the predicate returns 'true') will be serialized.
64 This avoids any potentially expensive computations.
66 See the SYNOPSIS for a nice example that can be easily cargo-culted.
80 All complex software has bugs lurking in it, and this module is no
81 exception. If you find a bug please either email me, or add the bug
86 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
88 =head1 COPYRIGHT AND LICENSE
90 Copyright 2007-2008 by Infinity Interactive, Inc.
92 L<http://www.iinteractive.com>
94 This library is free software; you can redistribute it and/or modify
95 it under the same terms as Perl itself.