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