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.50';
5              
6 1     1   19757 use Moose;
  1         424946  
  1         8  
7 1     1   7090 use MooseX::Storage::Engine ();
  1         3  
  1         39  
8 1     1   8 use Scalar::Util 'blessed';
  1         2  
  1         61  
9 1     1   6 use Carp 'confess';
  1         2  
  1         48  
10 1     1   847 use JSON::MaybeXS 1.001000;
  1         5602  
  1         55  
11 1     1   5 use namespace::autoclean;
  1         2  
  1         9  
12              
13             sub peek {
14 3     3 1 378 my ($class, $data, %options) = @_;
15              
16 3 100       10 if (exists $options{'format'}) {
17              
18 2         21 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         7 $data = $class->$inflater($data);
24             }
25              
26 3 50 33     60 (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     16 $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         1 eval { require JSON::MaybeXS; JSON::MaybeXS->import };
  1         7  
  1         44  
39 1 50       4 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       5 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       67 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         2 eval { require YAML::Any; YAML::Any->import };
  1         621  
  1         892  
57 1 50       3166 confess "Could not load YAML module because : $@" if $@;
58              
59 1         2 my $data = eval { Load($yaml) };
  1         4  
60 1 50       170 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   398 no Moose::Role;
  1         2  
  1         9  
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.50
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             =head2 Introspection
128              
129             =over 4
130              
131             =item B<meta>
132              
133             =back
134              
135             =for stopwords TODO
136              
137             =head1 TODO
138              
139             Add more stuff to this module :)
140              
141             =head1 BUGS
142              
143             All complex software has bugs lurking in it, and this module is no
144             exception. If you find a bug please or add the bug to cpan-RT
145             at L<https://rt.cpan.org/Dist/Display.html?Queue=MooseX-Storage>.
146              
147             =head1 AUTHORS
148              
149             =over 4
150              
151             =item *
152              
153             Chris Prather <chris.prather@iinteractive.com>
154              
155             =item *
156              
157             Stevan Little <stevan.little@iinteractive.com>
158              
159             =item *
160              
161             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
162              
163             =back
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is copyright (c) 2007 by Infinity Interactive, Inc..
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut