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