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