1 package MooseX::Storage::Util;
2 use Moose qw(confess blessed);
4 use MooseX::Storage::Engine ();
7 my ($class, $data, %options) = @_;
9 if (exists $options{'format'}) {
11 my $inflater = $class->can('_inflate_' . lc($options{'format'}));
14 || confess "No inflater found for " . $options{'format'};
16 $data = $class->$inflater($data);
19 (ref($data) && ref($data) eq 'HASH' && !blessed($data))
20 || confess "The data has to be a HASH reference, but not blessed";
22 $options{'key'} ||= $MooseX::Storage::Engine::CLASS_MARKER;
24 return $data->{$options{'key'}};
29 my ($self, $json) = @_;
31 eval { require JSON::Any; JSON::Any->import };
32 confess "Could not load JSON module because : $@" if $@;
34 utf8::encode($json) if utf8::is_utf8($json);
36 my $data = eval { JSON::Any->jsonToObj($json) };
38 confess "There was an error when attempting to peek at JSON: $@";
45 my ($self, $yaml) = @_;
47 eval { require YAML::Any; YAML::Any->import };
48 confess "Could not load YAML module because : $@" if $@;
50 my $data = eval { Load($yaml) };
52 confess "There was an error when attempting to peek at YAML : $@";
67 MooseX::Storage::Util - A MooseX::Storage Swiss Army chainsaw
71 This module provides a set of tools, some sharp and focused,
72 others more blunt and crude. But no matter what, they are useful
73 bits to have around when dealing with MooseX::Storage code.
77 All the methods in this package are class methods and should
78 be called appropriately.
82 =item B<peek ($data, %options)>
84 This method will help you to verify that the serialized class you
85 have gotten is what you expect it to be before you actually
88 The C<$data> can be either a perl HASH ref or some kind of serialized
89 data (JSON, YAML, etc.).
91 The C<%options> are as follows:
97 If this is left blank, we assume that C<$data> is a plain perl HASH ref
98 otherwise we attempt to inflate C<$data> based on the value of this option.
100 Currently only JSON and YAML are supported here.
104 The default is to try and extract the class name, but if you want to check
105 another key in the data, you can set this option. It will return the value
106 found in the key for you.
124 Add more stuff to this module :)
128 All complex software has bugs lurking in it, and this module is no
129 exception. If you find a bug please either email me, or add the bug
134 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
136 =head1 COPYRIGHT AND LICENSE
138 Copyright 2007-2008 by Infinity Interactive, Inc.
140 L<http://www.iinteractive.com>
142 This library is free software; you can redistribute it and/or modify
143 it under the same terms as Perl itself.