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