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