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