Commit | Line | Data |
124c2ba5 |
1 | package MooseX::Storage::IO::StorableFile; |
2 | use Moose::Role; |
3 | |
4 | use Storable (); |
5 | |
e7ab79ca |
6 | our $VERSION = '0.34'; |
124c2ba5 |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
9 | requires 'pack'; |
10 | requires 'unpack'; |
11 | |
12 | sub load { |
bf33d7c7 |
13 | my ( $class, $filename, @args ) = @_; |
124c2ba5 |
14 | # try thawing |
bf33d7c7 |
15 | return $class->thaw( Storable::retrieve($filename), @args ) |
ec725183 |
16 | if $class->can('thaw'); |
124c2ba5 |
17 | # otherwise just unpack |
bf33d7c7 |
18 | $class->unpack( Storable::retrieve($filename), @args ); |
124c2ba5 |
19 | } |
20 | |
21 | sub store { |
bf33d7c7 |
22 | my ( $self, $filename, @args ) = @_; |
ec725183 |
23 | Storable::nstore( |
124c2ba5 |
24 | # try freezing, otherwise just pack |
ec725183 |
25 | ($self->can('freeze') ? $self->freeze(@args) : $self->pack(@args)), |
26 | $filename |
124c2ba5 |
27 | ); |
28 | } |
29 | |
f82612bc |
30 | no Moose::Role; |
31 | |
124c2ba5 |
32 | 1; |
33 | |
34 | __END__ |
35 | |
36 | =pod |
37 | |
38 | =head1 NAME |
39 | |
4fa64e86 |
40 | MooseX::Storage::IO::StorableFile - An Storable File I/O role |
124c2ba5 |
41 | |
42 | =head1 SYNOPSIS |
43 | |
44 | package Point; |
45 | use Moose; |
46 | use MooseX::Storage; |
ec725183 |
47 | |
124c2ba5 |
48 | with Storage('io' => 'StorableFile'); |
ec725183 |
49 | |
124c2ba5 |
50 | has 'x' => (is => 'rw', isa => 'Int'); |
51 | has 'y' => (is => 'rw', isa => 'Int'); |
ec725183 |
52 | |
124c2ba5 |
53 | 1; |
ec725183 |
54 | |
124c2ba5 |
55 | my $p = Point->new(x => 10, y => 10); |
ec725183 |
56 | |
57 | ## methods to load/store a class |
124c2ba5 |
58 | ## on the file system |
ec725183 |
59 | |
124c2ba5 |
60 | $p->store('my_point'); |
ec725183 |
61 | |
124c2ba5 |
62 | my $p2 = Point->load('my_point'); |
63 | |
64 | =head1 DESCRIPTION |
65 | |
ec725183 |
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. |
124c2ba5 |
69 | |
ec725183 |
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. |
124c2ba5 |
73 | |
ec725183 |
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 |
124c2ba5 |
76 | will attempt to use C<freeze> or C<thaw> if that method is available. |
ec725183 |
77 | Of course, you should be careful when doing this as it could lead to |
124c2ba5 |
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 | |
ec725183 |
100 | All complex software has bugs lurking in it, and this module is no |
124c2ba5 |
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 | |
1f3074ea |
110 | Copyright 2007-2008 by Infinity Interactive, Inc. |
124c2ba5 |
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 | |