File Coverage

blib/lib/MooseX/Storage/Util.pm
Criterion Covered Total %
statement 48 50 96.0
branch 9 16 56.2
condition 3 9 33.3
subroutine 10 10 100.0
pod 1 1 100.0
total 71 86 82.5


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