Make false ints and strings work, bools still no good
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine.pm
CommitLineData
e59193fb 1
2package MooseX::Storage::Engine;
3use Moose;
4
69b45b7d 5our $VERSION = '0.02';
6our $AUTHORITY = 'cpan:STEVAN';
ba5bba75 7
8# the class marker when
9# serializing an object.
10our $CLASS_MARKER = '__CLASS__';
11
e59193fb 12has 'storage' => (
7eb5dc63 13 is => 'ro',
14 isa => 'HashRef',
15 default => sub {{}}
16);
17
18has 'seen' => (
19 is => 'ro',
e9739624 20 isa => 'HashRef',
e59193fb 21 default => sub {{}}
22);
23
e9739624 24has 'object' => (is => 'rw', isa => 'Object');
25has 'class' => (is => 'rw', isa => 'Str');
e59193fb 26
e9739624 27## this is the API used by other modules ...
e59193fb 28
29sub collapse_object {
34dcaa5d 30 my ( $self, %options ) = @_;
7eb5dc63 31
32 # NOTE:
33 # mark the root object as seen ...
34 $self->seen->{$self->object} = undef;
35
34dcaa5d 36 $self->map_attributes('collapse_attribute', \%options);
c1830046 37 $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;
e59193fb 38 return $self->storage;
39}
40
e9739624 41sub expand_object {
219c1cc5 42 my ($self, $data, %options) = @_;
43
44 $options{check_version} = 1 unless exists $options{check_version};
45 $options{check_authority} = 1 unless exists $options{check_authority};
7eb5dc63 46
47 # NOTE:
48 # mark the root object as seen ...
49 $self->seen->{$data} = undef;
50
219c1cc5 51 $self->map_attributes('expand_attribute', $data, \%options);
e9739624 52 return $self->storage;
e59193fb 53}
54
e9739624 55## this is the internal API ...
56
57sub collapse_attribute {
f6e1331f 58 my ($self, $attr, $options) = @_;
59 $self->storage->{$attr->name} = $self->collapse_attribute_value($attr, $options) || return;
e9739624 60}
61
62sub expand_attribute {
219c1cc5 63 my ($self, $attr, $data, $options) = @_;
14b0a4dc 64 my $value = $self->expand_attribute_value($attr, $data->{$attr->name}, $options);
65 $self->storage->{$attr->name} = defined $value ? $value : return;
e59193fb 66}
67
e9739624 68sub collapse_attribute_value {
f6e1331f 69 my ($self, $attr, $options) = @_;
e9739624 70 my $value = $attr->get_value($self->object);
7eb5dc63 71
b430caa3 72 # NOTE:
73 # this might not be enough, we might
74 # need to make it possible for the
75 # cycle checker to return the value
34dcaa5d 76 $self->check_for_cycle_in_collapse($attr, $value)
7eb5dc63 77 if ref $value;
78
e9739624 79 if (defined $value && $attr->has_type_constraint) {
3db8f791 80 my $type_converter = $self->find_type_handler($attr->type_constraint);
e9739624 81 (defined $type_converter)
82 || confess "Cannot convert " . $attr->type_constraint->name;
f6e1331f 83 $value = $type_converter->{collapse}->($value, $options);
e9739624 84 }
85 return $value;
86}
87
88sub expand_attribute_value {
219c1cc5 89 my ($self, $attr, $value, $options) = @_;
7eb5dc63 90
b430caa3 91 # NOTE:
92 # (see comment in method above ^^)
45d9a73c 93 $self->check_for_cycle_in_expansion($attr, $value)
7eb5dc63 94 if ref $value;
95
e9739624 96 if (defined $value && $attr->has_type_constraint) {
3db8f791 97 my $type_converter = $self->find_type_handler($attr->type_constraint);
219c1cc5 98 $value = $type_converter->{expand}->($value, $options);
e9739624 99 }
100 return $value;
101}
102
b430caa3 103# NOTE:
104# possibly these two methods will
105# be used by a cycle supporting
106# engine. However, I am not sure
107# if I can make a cycle one work
108# anyway.
e9739624 109
7eb5dc63 110sub check_for_cycle_in_collapse {
45d9a73c 111 my ($self, $attr, $value) = @_;
7eb5dc63 112 (!exists $self->seen->{$value})
45d9a73c 113 || confess "Basic Engine does not support cycles in class("
c1830046 114 . ($attr->associated_class->name) . ").attr("
45d9a73c 115 . ($attr->name) . ") with $value";
7eb5dc63 116 $self->seen->{$value} = undef;
117}
118
119sub check_for_cycle_in_expansion {
45d9a73c 120 my ($self, $attr, $value) = @_;
7eb5dc63 121 (!exists $self->seen->{$value})
45d9a73c 122 || confess "Basic Engine does not support cycles in class("
c1830046 123 . ($attr->associated_class->name) . ").attr("
45d9a73c 124 . ($attr->name) . ") with $value";
7eb5dc63 125 $self->seen->{$value} = undef;
126}
127
b430caa3 128# util methods ...
129
e9739624 130sub map_attributes {
131 my ($self, $method_name, @args) = @_;
132 map {
133 $self->$method_name($_, @args)
b430caa3 134 } grep {
135 # Skip our special skip attribute :)
136 !$_->isa('MooseX::Storage::Meta::Attribute::DoNotSerialize')
e9739624 137 } ($self->object || $self->class)->meta->compute_all_applicable_attributes;
e59193fb 138}
139
913d96dd 140## ------------------------------------------------------------------
3db8f791 141## This is all the type handler stuff, it is in a state of flux
142## right now, so this may change, or it may just continue to be
143## improved upon. Comments and suggestions are welcomed.
913d96dd 144## ------------------------------------------------------------------
145
146# NOTE:
147# these are needed by the
148# ArrayRef and HashRef handlers
149# below, so I need easy access
150my %OBJECT_HANDLERS = (
151 expand => sub {
219c1cc5 152 my ($data, $options) = @_;
ba5bba75 153 (exists $data->{$CLASS_MARKER})
913d96dd 154 || confess "Serialized item has no class marker";
c1830046 155 # check the class more thoroughly here ...
156 my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER});
157 my $meta = eval { $class->meta };
219c1cc5 158 confess "Class ($class) is not loaded, cannot unpack" if $@;
159
160 if ($options->{check_version}) {
161 my $meta_version = $meta->version;
162 if (defined $meta_version && $version) {
163 if ($options->{check_version} eq 'allow_less_than') {
164 ($meta_version <= $version)
165 || confess "Class ($class) versions is not less than currently available."
166 . " got=($version) available=($meta_version)";
167 }
168 elsif ($options->{check_version} eq 'allow_greater_than') {
169 ($meta->version >= $version)
170 || confess "Class ($class) versions is not greater than currently available."
171 . " got=($version) available=($meta_version)";
172 }
173 else {
174 ($meta->version == $version)
175 || confess "Class ($class) versions don't match."
176 . " got=($version) available=($meta_version)";
177 }
178 }
179 }
180
181 if ($options->{check_authority}) {
182 my $meta_authority = $meta->authority;
183 ($meta->authority eq $authority)
184 || confess "Class ($class) authorities don't match."
185 . " got=($authority) available=($meta_authority)"
186 if defined $meta_authority && defined $authority;
187 }
188
c1830046 189 # all is well ...
34dcaa5d 190 $class->unpack($data, %$options);
913d96dd 191 },
192 collapse => sub {
34dcaa5d 193 my ( $obj, $options ) = @_;
c4a322ec 194# ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
195# || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
1c6ac775 196 ($obj->can('pack'))
197 || confess "Object does not have a &pack method, cannot collapse";
34dcaa5d 198 $obj->pack(%$options);
913d96dd 199 },
200);
201
202
e59193fb 203my %TYPES = (
3db8f791 204 # These are boring ones, so they use the identity function ...
e9739624 205 'Int' => { expand => sub { shift }, collapse => sub { shift } },
206 'Num' => { expand => sub { shift }, collapse => sub { shift } },
207 'Str' => { expand => sub { shift }, collapse => sub { shift } },
d691721b 208 'Bool' => { expand => sub { shift }, collapse => sub { shift } },
3db8f791 209 # These are the trickier ones, (see notes)
210 # NOTE:
211 # Because we are nice guys, we will check
212 # your ArrayRef and/or HashRef one level
213 # down and inflate any objects we find.
214 # But this is where it ends, it is too
215 # expensive to try and do this any more
216 # recursively, when it is probably not
217 # nessecary in most of the use cases.
218 # However, if you need more then this, subtype
219 # and add a custom handler.
913d96dd 220 'ArrayRef' => {
e9739624 221 expand => sub {
34dcaa5d 222 my ( $array, @args ) = @_;
913d96dd 223 foreach my $i (0 .. $#{$array}) {
224 next unless ref($array->[$i]) eq 'HASH'
ba5bba75 225 && exists $array->[$i]->{$CLASS_MARKER};
34dcaa5d 226 $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
913d96dd 227 }
228 $array;
229 },
34dcaa5d 230 collapse => sub {
231 my ( $array, @args ) = @_;
913d96dd 232 # NOTE:
233 # we need to make a copy cause
234 # otherwise it will affect the
235 # other real version.
236 [ map {
237 blessed($_)
34dcaa5d 238 ? $OBJECT_HANDLERS{collapse}->($_, @args)
913d96dd 239 : $_
240 } @$array ]
241 }
242 },
243 'HashRef' => {
3db8f791 244 expand => sub {
34dcaa5d 245 my ( $hash, @args ) = @_;
3db8f791 246 foreach my $k (keys %$hash) {
247 next unless ref($hash->{$k}) eq 'HASH'
ba5bba75 248 && exists $hash->{$k}->{$CLASS_MARKER};
34dcaa5d 249 $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
3db8f791 250 }
251 $hash;
252 },
253 collapse => sub {
34dcaa5d 254 my ( $hash, @args ) = @_;
3db8f791 255 # NOTE:
256 # we need to make a copy cause
257 # otherwise it will affect the
258 # other real version.
259 +{ map {
95f31c36 260 blessed($hash->{$_})
f6e1331f 261 ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
3db8f791 262 : ($_ => $hash->{$_})
263 } keys %$hash }
264 }
e1bb45ff 265 },
913d96dd 266 'Object' => \%OBJECT_HANDLERS,
e1bb45ff 267 # NOTE:
268 # The sanity of enabling this feature by
269 # default is very questionable.
270 # - SL
271 #'CodeRef' => {
272 # expand => sub {}, # use eval ...
273 # collapse => sub {}, # use B::Deparse ...
7eb5dc63 274 #}
e59193fb 275);
276
3db8f791 277sub add_custom_type_handler {
278 my ($class, $type_name, %handlers) = @_;
279 (exists $handlers{expand} && exists $handlers{collapse})
280 || confess "Custom type handlers need an expand *and* a collapse method";
281 $TYPES{$type_name} = \%handlers;
282}
283
284sub remove_custom_type_handler {
285 my ($class, $type_name) = @_;
286 delete $TYPES{$type_name} if exists $TYPES{$type_name};
287}
288
289sub find_type_handler {
e59193fb 290 my ($self, $type_constraint) = @_;
e1bb45ff 291
292 # this should handle most type usages
293 # since they they are usually just
294 # the standard set of built-ins
295 return $TYPES{$type_constraint->name}
296 if exists $TYPES{$type_constraint->name};
297
298 # the next possibility is they are
299 # a subtype of the built-in types,
300 # in which case this will DWIM in
301 # most cases. It is probably not
302 # 100% ideal though, but until I
303 # come up with a decent test case
304 # it will do for now.
e59193fb 305 foreach my $type (keys %TYPES) {
306 return $TYPES{$type}
307 if $type_constraint->is_subtype_of($type);
308 }
e1bb45ff 309
310 # NOTE:
311 # the reason the above will work has to
312 # do with the fact that custom subtypes
313 # are mostly used for validation of
314 # the guts of a type, and not for some
315 # weird structural thing which would
316 # need to be accomidated by the serializer.
317 # Of course, mst or phaylon will probably
318 # do something to throw this assumption
319 # totally out the door ;)
320 # - SL
321
e9739624 322 # NOTE:
323 # if this method hasnt returned by now
324 # then we have no been able to find a
325 # type constraint handler to match
326 confess "Cannot handle type constraint (" . $type_constraint->name . ")";
e59193fb 327}
328
3291;
e9739624 330
331__END__
332
333=pod
334
ec9c1923 335=head1 NAME
336
1390c23d 337MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects
ec9c1923 338
339=head1 DESCRIPTION
340
1390c23d 341No user serviceable parts inside. If you really want to know, read the source :)
342
ec9c1923 343=head1 METHODS
344
345=head2 Accessors
346
347=over 4
348
349=item B<class>
350
351=item B<object>
352
353=item B<storage>
354
b430caa3 355=item B<seen>
356
ec9c1923 357=back
358
359=head2 API
360
361=over 4
362
363=item B<expand_object>
364
365=item B<collapse_object>
366
367=back
368
369=head2 ...
370
371=over 4
372
373=item B<collapse_attribute>
374
375=item B<collapse_attribute_value>
376
377=item B<expand_attribute>
378
379=item B<expand_attribute_value>
380
b430caa3 381=item B<check_for_cycle_in_collapse>
382
383=item B<check_for_cycle_in_expansion>
384
ec9c1923 385=item B<map_attributes>
386
3db8f791 387=back
388
389=head2 Type Constraint Handlers
390
391=over 4
392
393=item B<find_type_handler>
394
395=item B<add_custom_type_handler>
396
397=item B<remove_custom_type_handler>
ec9c1923 398
399=back
400
401=head2 Introspection
402
403=over 4
404
405=item B<meta>
406
407=back
408
409=head1 BUGS
410
411All complex software has bugs lurking in it, and this module is no
412exception. If you find a bug please either email me, or add the bug
413to cpan-RT.
414
415=head1 AUTHOR
416
417Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
418
419Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
420
421=head1 COPYRIGHT AND LICENSE
422
423Copyright 2007 by Infinity Interactive, Inc.
424
425L<http://www.iinteractive.com>
426
427This library is free software; you can redistribute it and/or modify
428it under the same terms as Perl itself.
429
e9739624 430=cut
431
432
ec9c1923 433