f5d2119c91a73380c462f182cb4d5f79c04486d7
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / IO / StorableFile.pm
1
2 package MooseX::Storage::IO::StorableFile;
3 use Moose::Role;
4
5 use Storable ();
6
7 our $VERSION   = '0.18';
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 requires 'pack';
11 requires 'unpack';
12
13 sub load {
14     my ( $class, $filename, @args ) = @_;
15     # try thawing
16     return $class->thaw( Storable::retrieve($filename), @args )
17         if $class->can('thaw');
18     # otherwise just unpack
19     $class->unpack( Storable::retrieve($filename), @args );
20 }
21
22 sub store {
23     my ( $self, $filename, @args ) = @_;
24     Storable::nstore(
25         # try freezing, otherwise just pack
26         ($self->can('freeze') ? $self->freeze(@args) : $self->pack(@args)),
27         $filename
28     );
29 }
30
31 1;
32
33 __END__
34
35 =pod
36
37 =head1 NAME
38
39 MooseX::Storage::IO::StorableFile - An Storable File I/O role
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
65 This module will C<load> and C<store> Moose classes using Storable. It
66 uses C<Storable::nstore> by default so that it can be easily used
67 across machines or just locally.
68
69 One important thing to note is that this module does not mix well
70 with the other Format modules. Since Storable serialized perl data
71 structures in it's own format, those roles are lagely unnecessary.
72
73 However, there is always the possibility that having a set of
74 C<freeze/thaw> hooks can be useful, so because of that this module
75 will attempt to use C<freeze> or C<thaw> if that method is available.
76 Of course, you should be careful when doing this as it could lead to
77 all 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
99 All complex software has bugs lurking in it, and this module is no
100 exception. If you find a bug please either email me, or add the bug
101 to cpan-RT.
102
103 =head1 AUTHOR
104
105 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
106
107 =head1 COPYRIGHT AND LICENSE
108
109 Copyright 2007-2008 by Infinity Interactive, Inc.
110
111 L<http://www.iinteractive.com>
112
113 This library is free software; you can redistribute it and/or modify
114 it under the same terms as Perl itself.
115
116 =cut
117
118