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