File Coverage

blib/lib/MooseX/Storage/Util.pm
Criterion Covered Total %
statement 45 47 95.7
branch 9 16 56.2
condition 3 9 33.3
subroutine 9 9 100.0
pod 1 1 100.0
total 67 82 81.7


line stmt bran cond sub pod time code
1             package MooseX::Storage::Util;
2             # ABSTRACT: A MooseX::Storage Swiss Army chainsaw
3              
4             our $VERSION = '0.53';
5              
6 1     1   57438 use Moose;
  1         430704  
  1         7  
7 1     1   7446 use MooseX::Storage::Engine ();
  1         4  
  1         32  
8 1     1   8 use Scalar::Util 'blessed';
  1         1  
  1         56  
9 1     1   6 use Carp 'confess';
  1         2  
  1         39  
10 1     1   506 use JSON::MaybeXS 1.001000;
  1         4957  
  1         51  
11 1     1   8 use namespace::autoclean;
  1         2  
  1         9  
12              
13             sub peek {
14 3     3 1 692 my ($class, $data, %options) = @_;
15              
16 3 100       10 if (exists $options{'format'}) {
17              
18 2         18 my $inflater = $class->can('_inflate_' . lc($options{'format'}));
19              
20             (defined $inflater)
21 2 50       6 || confess "No inflater found for " . $options{'format'};
22              
23 2         5 $data = $class->$inflater($data);
24             }
25              
26 3 50 33     26 (ref($data) && ref($data) eq 'HASH' && !blessed($data))
      33        
27             || confess "The data has to be a HASH reference, but not blessed";
28              
29 3   33     15 $options{'key'} ||= $MooseX::Storage::Engine::CLASS_MARKER;
30              
31 3         20 return $data->{$options{'key'}};
32              
33             }
34              
35             sub _inflate_json {
36 1     1   3 my ($self, $json) = @_;
37              
38 1         2 eval { require JSON::MaybeXS; JSON::MaybeXS->import };
  1         5  
  1         45  
39 1 50       5 confess "Could not load JSON module because : $@" if $@;
40              
41             # this is actually a bad idea, but for consistency, we'll have to keep
42             # doing it...
43 1 50       3 utf8::encode($json) if utf8::is_utf8($json);
44              
45 1         2 my $data = eval { JSON::MaybeXS->new({ utf8 => 1 })->decode($json) };
  1         4  
46 1 50       34 if ($@) {
47 0         0 confess "There was an error when attempting to peek at JSON: $@";
48             }
49              
50 1         3 return $data;
51             }
52              
53             sub _inflate_yaml {
54 1     1   3 my ($self, $yaml) = @_;
55              
56 1         2 eval { require YAML::Any; YAML::Any->import };
  1         396  
  1         810  
57 1 50       2855 confess "Could not load YAML module because : $@" if $@;
58              
59 1         3 my $data = eval { Load($yaml) };
  1         3  
60 1 50       166 if ($@) {
61 0         0 confess "There was an error when attempting to peek at YAML : $@";
62             }
63 1         3 return $data;
64             }
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             MooseX::Storage::Util - A MooseX::Storage Swiss Army chainsaw
77              
78             =head1 VERSION
79              
80             version 0.53
81              
82             =head1 DESCRIPTION
83              
84             This module provides a set of tools, some sharp and focused,
85             others more blunt and crude. But no matter what, they are useful
86             bits to have around when dealing with MooseX::Storage code.
87              
88             =head1 METHODS
89              
90             All the methods in this package are class methods and should
91             be called appropriately.
92              
93             =over 4
94              
95             =item B<peek ($data, %options)>
96              
97             This method will help you to verify that the serialized class you
98             have gotten is what you expect it to be before you actually
99             unfreeze/unpack it.
100              
101             The C<$data> can be either a perl HASH ref or some kind of serialized
102             data (JSON, YAML, etc.).
103              
104             The C<%options> are as follows:
105              
106             =over 4
107              
108             =item I<format>
109              
110             If this is left blank, we assume that C<$data> is a plain perl HASH ref
111             otherwise we attempt to inflate C<$data> based on the value of this option.
112              
113             Currently only JSON and YAML are supported here.
114              
115             =item I<key>
116              
117             The default is to try and extract the class name, but if you want to check
118             another key in the data, you can set this option. It will return the value
119             found in the key for you.
120              
121             =back
122              
123             =back
124              
125             =for stopwords TODO
126              
127             =head1 TODO
128              
129             Add more stuff to this module :)
130              
131             =head1 SUPPORT
132              
133             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Storage>
134             (or L<bug-MooseX-Storage@rt.cpan.org|mailto:bug-MooseX-Storage@rt.cpan.org>).
135              
136             There is also a mailing list available for users of this distribution, at
137             L<http://lists.perl.org/list/moose.html>.
138              
139             There is also an irc channel available for users of this distribution, at
140             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
141              
142             =head1 AUTHORS
143              
144             =over 4
145              
146             =item *
147              
148             Chris Prather <chris.prather@iinteractive.com>
149              
150             =item *
151              
152             Stevan Little <stevan.little@iinteractive.com>
153              
154             =item *
155              
156             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
157              
158             =back
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is copyright (c) 2007 by Infinity Interactive, Inc.
163              
164             This is free software; you can redistribute it and/or modify it under
165             the same terms as the Perl 5 programming language system itself.
166              
167             =cut