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