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