File Coverage

blib/lib/MooseX/Storage/IO/StorableFile.pm
Criterion Covered Total %
statement 17 17 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 29 29 100.0


line stmt bran cond sub pod time code
1             package MooseX::Storage::IO::StorableFile;
2             # ABSTRACT: An Storable File I/O role
3              
4             our $VERSION = '0.50';
5              
6 2     2   1612 use Moose::Role;
  2         3  
  2         17  
7 2     2   10993 use Storable ();
  2         3149  
  2         45  
8 2     2   12 use namespace::autoclean;
  2         3  
  2         19  
9              
10             requires 'pack';
11             requires 'unpack';
12              
13             sub load {
14 2     2 1 20992 my ( $class, $filename, @args ) = @_;
15             # try thawing
16 2 100       22 return $class->thaw( Storable::retrieve($filename), @args )
17             if $class->can('thaw');
18             # otherwise just unpack
19 1         6 $class->unpack( Storable::retrieve($filename), @args );
20             }
21              
22             sub store {
23 2     2 1 13772 my ( $self, $filename, @args ) = @_;
24 2 100       35 Storable::nstore(
25             # try freezing, otherwise just pack
26             ($self->can('freeze') ? $self->freeze(@args) : $self->pack(@args)),
27             $filename
28             );
29             }
30              
31 2     2   379 no Moose::Role;
  2         7  
  2         11  
32              
33             1;
34              
35             __END__
36              
37             =pod
38              
39             =encoding UTF-8
40              
41             =head1 NAME
42              
43             MooseX::Storage::IO::StorableFile - An Storable File I/O role
44              
45             =head1 VERSION
46              
47             version 0.50
48              
49             =head1 SYNOPSIS
50              
51             package Point;
52             use Moose;
53             use MooseX::Storage;
54              
55             with Storage('io' => 'StorableFile');
56              
57             has 'x' => (is => 'rw', isa => 'Int');
58             has 'y' => (is => 'rw', isa => 'Int');
59              
60             1;
61              
62             my $p = Point->new(x => 10, y => 10);
63              
64             ## methods to load/store a class
65             ## on the file system
66              
67             $p->store('my_point');
68              
69             my $p2 = Point->load('my_point');
70              
71             =head1 DESCRIPTION
72              
73             This module will C<load> and C<store> Moose classes using Storable. It
74             uses C<Storable::nstore> by default so that it can be easily used
75             across machines or just locally.
76              
77             One important thing to note is that this module does not mix well
78             with the other Format modules. Since Storable serialized perl data
79             structures in it's own format, those roles are largely unnecessary.
80              
81             However, there is always the possibility that having a set of
82             C<freeze/thaw> hooks can be useful, so because of that this module
83             will attempt to use C<freeze> or C<thaw> if that method is available.
84             Of course, you should be careful when doing this as it could lead to
85             all sorts of hairy issues. But you have been warned.
86              
87             =head1 METHODS
88              
89             =over 4
90              
91             =item B<load ($filename)>
92              
93             =item B<store ($filename)>
94              
95             =back
96              
97             =head2 Introspection
98              
99             =over 4
100              
101             =item B<meta>
102              
103             =back
104              
105             =head1 BUGS
106              
107             All complex software has bugs lurking in it, and this module is no
108             exception. If you find a bug please or add the bug to cpan-RT
109             at L<https://rt.cpan.org/Dist/Display.html?Queue=MooseX-Storage>.
110              
111             =head1 AUTHORS
112              
113             =over 4
114              
115             =item *
116              
117             Chris Prather <chris.prather@iinteractive.com>
118              
119             =item *
120              
121             Stevan Little <stevan.little@iinteractive.com>
122              
123             =item *
124              
125             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
126              
127             =back
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is copyright (c) 2007 by Infinity Interactive, Inc..
132              
133             This is free software; you can redistribute it and/or modify it under
134             the same terms as the Perl 5 programming language system itself.
135              
136             =cut