Strip all the trailing whitespace
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Traits / OnlyWhenBuilt.pm
CommitLineData
76218b46 1package MooseX::Storage::Traits::OnlyWhenBuilt;
2use Moose::Role;
3
4our $VERSION = '0.18';
5our $AUTHORITY = 'cpan:STEVAN';
6
59abaf70 7requires 'pack';
8requires 'unpack';
9
10around 'pack' => sub {
11 my ($orig, $self, %args) = @_;
12 $args{engine_traits} ||= [];
13 push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
14 $self->$orig(%args);
15};
16
17around 'unpack' => sub {
18 my ($orig, $self, $data, %args) = @_;
19 $args{engine_traits} ||= [];
20 push(@{$args{engine_traits}}, 'OnlyWhenBuilt');
21 $self->$orig($data, %args);
22};
23
76218b46 241;
25
26__END__
27
28=pod
29
30=head1 NAME
31
32MooseX::Storage::Traits::OnlyWhenBuilt - A custom trait to bypass serialization
33
34=head1 SYNOPSIS
35
36
37 { package Point;
38 use Moose;
39 use MooseX::Storage;
ec725183 40
76218b46 41 with Storage( traits => [qw|OnlyWhenBuilt|] );
ec725183 42
76218b46 43 has 'x' => (is => 'rw', lazy_build => 1 );
44 has 'y' => (is => 'rw', lazy_build => 1 );
45 has 'z' => (is => 'rw', builder => '_build_z' );
ec725183 46
76218b46 47 sub _build_x { 3 }
48 sub _build_y { expensive_computation() }
49 sub _build_z { 3 }
ec725183 50
76218b46 51 }
ec725183 52
76218b46 53 my $p = Point->new( 'x' => 4 );
ec725183 54
76218b46 55 # the result of ->pack will contain:
56 # { x => 4, z => 3 }
57 $p->pack;
ec725183 58
76218b46 59=head1 DESCRIPTION
60
ec725183 61Sometimes you don't want a particular attribute to be part of the
76218b46 62serialization if it has not been built yet. If you invoke C<Storage()>
63as outlined in the C<Synopsis>, only attributes that have been built
64(ie, where the predicate returns 'true') will be serialized.
65This avoids any potentially expensive computations.
66
67See the SYNOPSIS for a nice example that can be easily cargo-culted.
68
69=head1 METHODS
70
71=head2 Introspection
72
73=over 4
74
75=item B<meta>
76
77=back
78
79=head1 BUGS
80
ec725183 81All complex software has bugs lurking in it, and this module is no
76218b46 82exception. If you find a bug please either email me, or add the bug
83to cpan-RT.
84
85=head1 AUTHOR
86
87Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
88
89=head1 COPYRIGHT AND LICENSE
90
91Copyright 2007-2008 by Infinity Interactive, Inc.
92
93L<http://www.iinteractive.com>
94
95This library is free software; you can redistribute it and/or modify
96it under the same terms as Perl itself.
97
98=cut