Bump versions
[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
d4b1b667 7our $VERSION = '0.20';
124c2ba5 8our $AUTHORITY = 'cpan:STEVAN';
9
10requires 'pack';
11requires 'unpack';
12
13sub load {
bf33d7c7 14 my ( $class, $filename, @args ) = @_;
124c2ba5 15 # try thawing
bf33d7c7 16 return $class->thaw( Storable::retrieve($filename), @args )
ec725183 17 if $class->can('thaw');
124c2ba5 18 # otherwise just unpack
bf33d7c7 19 $class->unpack( Storable::retrieve($filename), @args );
124c2ba5 20}
21
22sub store {
bf33d7c7 23 my ( $self, $filename, @args ) = @_;
ec725183 24 Storable::nstore(
124c2ba5 25 # try freezing, otherwise just pack
ec725183 26 ($self->can('freeze') ? $self->freeze(@args) : $self->pack(@args)),
27 $filename
124c2ba5 28 );
29}
30
311;
32
33__END__
34
35=pod
36
37=head1 NAME
38
4fa64e86 39MooseX::Storage::IO::StorableFile - An Storable File I/O role
124c2ba5 40
41=head1 SYNOPSIS
42
43 package Point;
44 use Moose;
45 use MooseX::Storage;
ec725183 46
124c2ba5 47 with Storage('io' => 'StorableFile');
ec725183 48
124c2ba5 49 has 'x' => (is => 'rw', isa => 'Int');
50 has 'y' => (is => 'rw', isa => 'Int');
ec725183 51
124c2ba5 52 1;
ec725183 53
124c2ba5 54 my $p = Point->new(x => 10, y => 10);
ec725183 55
56 ## methods to load/store a class
124c2ba5 57 ## on the file system
ec725183 58
124c2ba5 59 $p->store('my_point');
ec725183 60
124c2ba5 61 my $p2 = Point->load('my_point');
62
63=head1 DESCRIPTION
64
ec725183 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.
124c2ba5 68
ec725183 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.
124c2ba5 72
ec725183 73However, there is always the possibility that having a set of
74C<freeze/thaw> hooks can be useful, so because of that this module
124c2ba5 75will attempt to use C<freeze> or C<thaw> if that method is available.
ec725183 76Of course, you should be careful when doing this as it could lead to
124c2ba5 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
ec725183 99All complex software has bugs lurking in it, and this module is no
124c2ba5 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