1 package MooseX::Storage::Util;
2 use Moose qw(confess blessed);
4 use MooseX::Storage::Engine ();
7 our $AUTHORITY = 'cpan:STEVAN';
10 my ($class, $data, %options) = @_;
12 if (exists $options{'format'}) {
14 my $inflater = $class->can('_inflate_' . lc($options{'format'}));
17 || confess "No inflater found for " . $options{'format'};
19 $data = $class->$inflater($data);
22 (ref($data) && ref($data) eq 'HASH' && !blessed($data))
23 || confess "The data has to be a HASH reference, but not blessed";
25 $options{'key'} ||= $MooseX::Storage::Engine::CLASS_MARKER;
27 return $data->{$options{'key'}};
32 my ($class, $json) = @_;
37 my $data = eval { JSON::Any->jsonToObj($json) };
39 confess "There was an error when attempting to peek at JSON: $@";
46 my ($class, $yaml) = @_;
49 Best->import([[ qw[YAML::Syck YAML] ]]);
51 my $inflater = Best->which('YAML::Syck')->can('Load');
54 || confess "Could not load the YAML inflator";
56 my $data = eval { $inflater->($yaml) };
58 confess "There was an error when attempting to peek at YAML : $@";
71 MooseX::Storage::Util - A MooseX::Storage swiss-army chainsaw
75 This module provides a set of tools, some sharp and focused,
76 others more blunt and crude. But no matter what, they are useful
77 bits to have around when dealing with MooseX::Storage code.
81 All the methods in this package are class methods and should
82 be called appropriately.
86 =item B<peek ($data, %options)>
88 This method will help you to verify that the serialized class you
89 have gotten is what you expect it to be before you actually
92 The C<$data> can be either a perl HASH ref or some kind of serialized
93 data (JSON, YAML, etc.).
95 The C<%options> are as follows:
101 If this is left blank, we assume that C<$data> is a plain perl HASH ref
102 otherwise we attempt to inflate C<$data> based on the value of this option.
104 Currently only JSON and YAML are supported here.
108 The default is to try and extract the class name, but if you want to check
109 another key in the data, you can set this option. It will return the value
110 found in the key for you.
126 Add more stuff to this module :)
130 All complex software has bugs lurking in it, and this module is no
131 exception. If you find a bug please either email me, or add the bug
136 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
138 =head1 COPYRIGHT AND LICENSE
140 Copyright 2007 by Infinity Interactive, Inc.
142 L<http://www.iinteractive.com>
144 This library is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself.