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