1 package MooseX::Storage::Util;
2 use Moose qw(confess blessed);
4 use MooseX::Storage::Engine ();
8 our $AUTHORITY = 'cpan:STEVAN';
11 my ($class, $data, %options) = @_;
13 if (exists $options{'format'}) {
15 my $inflater = $class->can('_inflate_' . lc($options{'format'}));
18 || confess "No inflater found for " . $options{'format'};
20 $data = $class->$inflater($data);
23 (ref($data) && ref($data) eq 'HASH' && !blessed($data))
24 || confess "The data has to be a HASH reference, but not blessed";
26 $options{'key'} ||= $MooseX::Storage::Engine::CLASS_MARKER;
28 return $data->{$options{'key'}};
33 my ($class, $json) = @_;
35 eval { require JSON::Any; JSON::Any->import };
36 confess "Could not load JSON module because : $@" if $@;
38 utf8::encode($json) if utf8::is_utf8($json);
40 my $data = eval { JSON::Any->jsonToObj($json) };
42 confess "There was an error when attempting to peek at JSON: $@";
49 my ($class, $yaml) = @_;
52 eval { Best->import([[ qw[YAML::Syck YAML] ]]) };
53 confess "Could not load YAML module because : $@" if $@;
55 my $inflater = Best->which('YAML::Syck')->can('Load');
58 || confess "Could not load the YAML inflator";
60 my $data = eval { $inflater->($yaml) };
62 confess "There was an error when attempting to peek at YAML : $@";
68 my ($class, $xml) = @_;
70 eval { require XML::Simple; XML::Simple->import };
71 confess "Could not load XML::Simple module because : $@" if $@;
73 my $data = eval { XMLin($xml, SuppressEmpty => 1) };
75 confess "There was an error when attempting to peek at XML: $@";
89 MooseX::Storage::Util - A MooseX::Storage swiss-army chainsaw
93 This module provides a set of tools, some sharp and focused,
94 others more blunt and crude. But no matter what, they are useful
95 bits to have around when dealing with MooseX::Storage code.
99 All the methods in this package are class methods and should
100 be called appropriately.
104 =item B<peek ($data, %options)>
106 This method will help you to verify that the serialized class you
107 have gotten is what you expect it to be before you actually
110 The C<$data> can be either a perl HASH ref or some kind of serialized
111 data (JSON, YAML, etc.).
113 The C<%options> are as follows:
119 If this is left blank, we assume that C<$data> is a plain perl HASH ref
120 otherwise we attempt to inflate C<$data> based on the value of this option.
122 Currently only JSON and YAML are supported here.
126 The default is to try and extract the class name, but if you want to check
127 another key in the data, you can set this option. It will return the value
128 found in the key for you.
144 Add more stuff to this module :)
148 All complex software has bugs lurking in it, and this module is no
149 exception. If you find a bug please either email me, or add the bug
154 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
156 =head1 COPYRIGHT AND LICENSE
158 Copyright 2007-2008 by Infinity Interactive, Inc.
160 L<http://www.iinteractive.com>
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.