adding support for Deferred
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / IO / StorableFile.pm
CommitLineData
124c2ba5 1
2package MooseX::Storage::IO::StorableFile;
3use Moose::Role;
4
5use Storable ();
6
7our $VERSION = '0.01';
8our $AUTHORITY = 'cpan:STEVAN';
9
10requires 'pack';
11requires 'unpack';
12
13sub load {
14 my ( $class, $filename ) = @_;
15 # try thawing
16 return $class->thaw( Storable::retrieve($filename) )
17 if $class->can('thaw');
18 # otherwise just unpack
19 $class->unpack( Storable::retrieve($filename) );
20}
21
22sub store {
23 my ( $self, $filename ) = @_;
24 Storable::nstore(
25 # try freezing, otherwise just pack
26 ($self->can('freeze') ? $self->freeze() : $self->pack()),
27 $filename
28 );
29}
30
311;
32
33__END__
34
35=pod
36
37=head1 NAME
38
39MooseX::Storage::IO::StorableFile
40
41=head1 SYNOPSIS
42
43 package Point;
44 use Moose;
45 use MooseX::Storage;
46
47 with Storage('io' => 'StorableFile');
48
49 has 'x' => (is => 'rw', isa => 'Int');
50 has 'y' => (is => 'rw', isa => 'Int');
51
52 1;
53
54 my $p = Point->new(x => 10, y => 10);
55
56 ## methods to load/store a class
57 ## on the file system
58
59 $p->store('my_point');
60
61 my $p2 = Point->load('my_point');
62
63=head1 DESCRIPTION
64
65This module will C<load> and C<store> Moose classes using Storable. It
66uses C<Storable::nstore> by default so that it can be easily used
67across machines or just locally.
68
69One important thing to note is that this module does not mix well
70with the other Format modules. Since Storable serialized perl data
71structures in it's own format, those roles are lagely unnecessary.
72
73However, there is always the possibility that having a set of
74C<freeze/thaw> hooks can be useful, so because of that this module
75will attempt to use C<freeze> or C<thaw> if that method is available.
76Of course, you should be careful when doing this as it could lead to
77all sorts of hairy issues. But you have been warned.
78
79=head1 METHODS
80
81=over 4
82
83=item B<load ($filename)>
84
85=item B<store ($filename)>
86
87=back
88
89=head2 Introspection
90
91=over 4
92
93=item B<meta>
94
95=back
96
97=head1 BUGS
98
99All complex software has bugs lurking in it, and this module is no
100exception. If you find a bug please either email me, or add the bug
101to cpan-RT.
102
103=head1 AUTHOR
104
105Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
106
107=head1 COPYRIGHT AND LICENSE
108
1f3074ea 109Copyright 2007-2008 by Infinity Interactive, Inc.
124c2ba5 110
111L<http://www.iinteractive.com>
112
113This library is free software; you can redistribute it and/or modify
114it under the same terms as Perl itself.
115
116=cut
117
118