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