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