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