Bump version numbers for release
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Util.pm
1 package MooseX::Storage::Util;
2 use Moose qw(confess blessed);
3
4 use MooseX::Storage::Engine ();
5 use utf8 ();
6
7 our $VERSION   = '0.18';
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 sub peek {
11     my ($class, $data, %options) = @_;
12     
13     if (exists $options{'format'}) {
14         
15         my $inflater = $class->can('_inflate_' . lc($options{'format'}));
16         
17         (defined $inflater)
18             || confess "No inflater found for " . $options{'format'};
19             
20         $data = $class->$inflater($data);
21     }
22
23     (ref($data) && ref($data) eq 'HASH' && !blessed($data))
24         || confess "The data has to be a HASH reference, but not blessed";
25     
26     $options{'key'} ||= $MooseX::Storage::Engine::CLASS_MARKER;
27     
28     return $data->{$options{'key'}};
29
30 }
31
32 sub _inflate_json {
33     my ($class, $json) = @_;
34
35     eval { require JSON::Any; JSON::Any->import };
36     confess "Could not load JSON module because : $@" if $@; 
37     
38     utf8::encode($json) if utf8::is_utf8($json);    
39     
40     my $data = eval { JSON::Any->jsonToObj($json) };
41     if ($@) {
42         confess "There was an error when attempting to peek at JSON: $@";
43     }
44     
45     return $data;
46 }
47
48 sub _inflate_yaml {
49     my ($class, $yaml) = @_;
50     
51     require Best; 
52     eval { Best->import([[ qw[YAML::Syck YAML] ]]) };
53     confess "Could not load YAML module because : $@" if $@; 
54     
55     my $inflater = Best->which('YAML::Syck')->can('Load');
56     
57     (defined $inflater)
58         || confess "Could not load the YAML inflator";
59     
60     my $data = eval { $inflater->($yaml) };
61     if ($@) {
62         confess "There was an error when attempting to peek at YAML : $@";
63     }
64     return $data;
65 }
66
67 1;
68
69 __END__
70
71 =pod
72
73 =head1 NAME
74
75 MooseX::Storage::Util - A MooseX::Storage swiss-army chainsaw
76
77 =head1 DESCRIPTION
78
79 This module provides a set of tools, some sharp and focused, 
80 others more blunt and crude. But no matter what, they are useful
81 bits to have around when dealing with MooseX::Storage code. 
82
83 =head1 METHODS
84
85 All the methods in this package are class methods and should 
86 be called appropriately. 
87
88 =over 4
89
90 =item B<peek ($data, %options)>
91
92 This method will help you to verify that the serialized class you 
93 have gotten is what you expect it to be before you actually 
94 unfreeze/unpack it.
95
96 The C<$data> can be either a perl HASH ref or some kind of serialized
97 data (JSON, YAML, etc.).
98
99 The C<%options> are as follows:
100
101 =over 4
102
103 =item I<format>
104
105 If this is left blank, we assume that C<$data> is a plain perl HASH ref
106 otherwise we attempt to inflate C<$data> based on the value of this option.
107
108 Currently only JSON and YAML are supported here.
109
110 =item I<key>
111
112 The default is to try and extract the class name, but if you want to check 
113 another key in the data, you can set this option. It will return the value
114 found in the key for you.
115
116 =back
117
118 =back
119
120 =head2 Introspection
121
122 =over 4
123
124 =item B<meta>
125
126 =back
127
128 =head1 TODO
129
130 Add more stuff to this module :)
131
132 =head1 BUGS
133
134 All complex software has bugs lurking in it, and this module is no 
135 exception. If you find a bug please either email me, or add the bug
136 to cpan-RT.
137
138 =head1 AUTHOR
139
140 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
141
142 =head1 COPYRIGHT AND LICENSE
143
144 Copyright 2007-2008 by Infinity Interactive, Inc.
145
146 L<http://www.iinteractive.com>
147
148 This library is free software; you can redistribute it and/or modify
149 it under the same terms as Perl itself.
150
151 =cut