remove last use of Best (that should have come out in 2010!)
[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
6 sub peek {
7     my ($class, $data, %options) = @_;
8
9     if (exists $options{'format'}) {
10
11         my $inflater = $class->can('_inflate_' . lc($options{'format'}));
12
13         (defined $inflater)
14             || confess "No inflater found for " . $options{'format'};
15
16         $data = $class->$inflater($data);
17     }
18
19     (ref($data) && ref($data) eq 'HASH' && !blessed($data))
20         || confess "The data has to be a HASH reference, but not blessed";
21
22     $options{'key'} ||= $MooseX::Storage::Engine::CLASS_MARKER;
23
24     return $data->{$options{'key'}};
25
26 }
27
28 sub _inflate_json {
29     my ($self, $json) = @_;
30
31     eval { require JSON::Any; JSON::Any->import };
32     confess "Could not load JSON module because : $@" if $@;
33
34     utf8::encode($json) if utf8::is_utf8($json);
35
36     my $data = eval { JSON::Any->jsonToObj($json) };
37     if ($@) {
38         confess "There was an error when attempting to peek at JSON: $@";
39     }
40
41     return $data;
42 }
43
44 sub _inflate_yaml {
45     my ($self, $yaml) = @_;
46
47     eval { require YAML::Any; YAML::Any->import };
48     confess "Could not load YAML module because : $@" if $@;
49
50     my $data = eval { Load($yaml) };
51     if ($@) {
52         confess "There was an error when attempting to peek at YAML : $@";
53     }
54     return $data;
55 }
56
57 no Moose::Role;
58
59 1;
60
61 __END__
62
63 =pod
64
65 =head1 NAME
66
67 MooseX::Storage::Util - A MooseX::Storage Swiss Army chainsaw
68
69 =head1 DESCRIPTION
70
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.
74
75 =head1 METHODS
76
77 All the methods in this package are class methods and should
78 be called appropriately.
79
80 =over 4
81
82 =item B<peek ($data, %options)>
83
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
86 unfreeze/unpack it.
87
88 The C<$data> can be either a perl HASH ref or some kind of serialized
89 data (JSON, YAML, etc.).
90
91 The C<%options> are as follows:
92
93 =over 4
94
95 =item I<format>
96
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.
99
100 Currently only JSON and YAML are supported here.
101
102 =item I<key>
103
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.
107
108 =back
109
110 =back
111
112 =head2 Introspection
113
114 =over 4
115
116 =item B<meta>
117
118 =back
119
120 =for stopwords TODO
121
122 =head1 TODO
123
124 Add more stuff to this module :)
125
126 =head1 BUGS
127
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
130 to cpan-RT.
131
132 =head1 AUTHOR
133
134 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
135
136 =head1 COPYRIGHT AND LICENSE
137
138 Copyright 2007-2008 by Infinity Interactive, Inc.
139
140 L<http://www.iinteractive.com>
141
142 This library is free software; you can redistribute it and/or modify
143 it under the same terms as Perl itself.
144
145 =cut