2 package MooseX::Storage::Engine;
7 # the class marker when
8 # serializing an object.
9 our $CLASS_MARKER = '__CLASS__';
23 has 'object' => (is => 'rw', isa => 'Object');
24 has 'class' => (is => 'rw', isa => 'Str');
26 ## this is the API used by other modules ...
32 # mark the root object as seen ...
33 $self->seen->{$self->object} = undef;
35 $self->map_attributes('collapse_attribute');
36 $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;
37 return $self->storage;
41 my ($self, $data, %options) = @_;
43 $options{check_version} = 1 unless exists $options{check_version};
44 $options{check_authority} = 1 unless exists $options{check_authority};
47 # mark the root object as seen ...
48 $self->seen->{$data} = undef;
50 $self->map_attributes('expand_attribute', $data, \%options);
51 return $self->storage;
54 ## this is the internal API ...
56 sub collapse_attribute {
57 my ($self, $attr) = @_;
58 $self->storage->{$attr->name} = $self->collapse_attribute_value($attr) || return;
61 sub expand_attribute {
62 my ($self, $attr, $data, $options) = @_;
63 $self->storage->{$attr->name} = $self->expand_attribute_value($attr, $data->{$attr->name}, $options) || return;
66 sub collapse_attribute_value {
67 my ($self, $attr) = @_;
68 my $value = $attr->get_value($self->object);
71 # this might not be enough, we might
72 # need to make it possible for the
73 # cycle checker to return the value
74 $self->check_for_cycle_in_collapse($attr, $value)
77 if (defined $value && $attr->has_type_constraint) {
78 my $type_converter = $self->find_type_handler($attr->type_constraint);
79 (defined $type_converter)
80 || confess "Cannot convert " . $attr->type_constraint->name;
81 $value = $type_converter->{collapse}->($value);
86 sub expand_attribute_value {
87 my ($self, $attr, $value, $options) = @_;
90 # (see comment in method above ^^)
91 $self->check_for_cycle_in_expansion($attr, $value)
94 if (defined $value && $attr->has_type_constraint) {
95 my $type_converter = $self->find_type_handler($attr->type_constraint);
96 $value = $type_converter->{expand}->($value, $options);
102 # possibly these two methods will
103 # be used by a cycle supporting
104 # engine. However, I am not sure
105 # if I can make a cycle one work
108 sub check_for_cycle_in_collapse {
109 my ($self, $attr, $value) = @_;
110 (!exists $self->seen->{$value})
111 || confess "Basic Engine does not support cycles in class("
112 . ($attr->associated_class->name) . ").attr("
113 . ($attr->name) . ") with $value";
114 $self->seen->{$value} = undef;
117 sub check_for_cycle_in_expansion {
118 my ($self, $attr, $value) = @_;
119 (!exists $self->seen->{$value})
120 || confess "Basic Engine does not support cycles in class("
121 . ($attr->associated_class->name) . ").attr("
122 . ($attr->name) . ") with $value";
123 $self->seen->{$value} = undef;
129 my ($self, $method_name, @args) = @_;
131 $self->$method_name($_, @args)
133 # Skip our special skip attribute :)
134 !$_->isa('MooseX::Storage::Meta::Attribute::DoNotSerialize')
135 } ($self->object || $self->class)->meta->compute_all_applicable_attributes;
138 ## ------------------------------------------------------------------
139 ## This is all the type handler stuff, it is in a state of flux
140 ## right now, so this may change, or it may just continue to be
141 ## improved upon. Comments and suggestions are welcomed.
142 ## ------------------------------------------------------------------
145 # these are needed by the
146 # ArrayRef and HashRef handlers
147 # below, so I need easy access
148 my %OBJECT_HANDLERS = (
150 my ($data, $options) = @_;
151 (exists $data->{$CLASS_MARKER})
152 || confess "Serialized item has no class marker";
153 # check the class more thoroughly here ...
154 my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER});
155 my $meta = eval { $class->meta };
156 confess "Class ($class) is not loaded, cannot unpack" if $@;
158 if ($options->{check_version}) {
159 my $meta_version = $meta->version;
160 if (defined $meta_version && $version) {
161 if ($options->{check_version} eq 'allow_less_than') {
162 ($meta_version <= $version)
163 || confess "Class ($class) versions is not less than currently available."
164 . " got=($version) available=($meta_version)";
166 elsif ($options->{check_version} eq 'allow_greater_than') {
167 ($meta->version >= $version)
168 || confess "Class ($class) versions is not greater than currently available."
169 . " got=($version) available=($meta_version)";
172 ($meta->version == $version)
173 || confess "Class ($class) versions don't match."
174 . " got=($version) available=($meta_version)";
179 if ($options->{check_authority}) {
180 my $meta_authority = $meta->authority;
181 ($meta->authority eq $authority)
182 || confess "Class ($class) authorities don't match."
183 . " got=($authority) available=($meta_authority)"
184 if defined $meta_authority && defined $authority;
188 $class->unpack($data);
192 # ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
193 # || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
200 # These are boring ones, so they use the identity function ...
201 'Int' => { expand => sub { shift }, collapse => sub { shift } },
202 'Num' => { expand => sub { shift }, collapse => sub { shift } },
203 'Str' => { expand => sub { shift }, collapse => sub { shift } },
204 # These are the trickier ones, (see notes)
206 # Because we are nice guys, we will check
207 # your ArrayRef and/or HashRef one level
208 # down and inflate any objects we find.
209 # But this is where it ends, it is too
210 # expensive to try and do this any more
211 # recursively, when it is probably not
212 # nessecary in most of the use cases.
213 # However, if you need more then this, subtype
214 # and add a custom handler.
218 foreach my $i (0 .. $#{$array}) {
219 next unless ref($array->[$i]) eq 'HASH'
220 && exists $array->[$i]->{$CLASS_MARKER};
221 $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i])
228 # we need to make a copy cause
229 # otherwise it will affect the
230 # other real version.
233 ? $OBJECT_HANDLERS{collapse}->($_)
241 foreach my $k (keys %$hash) {
242 next unless ref($hash->{$k}) eq 'HASH'
243 && exists $hash->{$k}->{$CLASS_MARKER};
244 $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k})
251 # we need to make a copy cause
252 # otherwise it will affect the
253 # other real version.
256 ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}))
257 : ($_ => $hash->{$_})
261 'Object' => \%OBJECT_HANDLERS,
263 # The sanity of enabling this feature by
264 # default is very questionable.
267 # expand => sub {}, # use eval ...
268 # collapse => sub {}, # use B::Deparse ...
272 sub add_custom_type_handler {
273 my ($class, $type_name, %handlers) = @_;
274 (exists $handlers{expand} && exists $handlers{collapse})
275 || confess "Custom type handlers need an expand *and* a collapse method";
276 $TYPES{$type_name} = \%handlers;
279 sub remove_custom_type_handler {
280 my ($class, $type_name) = @_;
281 delete $TYPES{$type_name} if exists $TYPES{$type_name};
284 sub find_type_handler {
285 my ($self, $type_constraint) = @_;
287 # this should handle most type usages
288 # since they they are usually just
289 # the standard set of built-ins
290 return $TYPES{$type_constraint->name}
291 if exists $TYPES{$type_constraint->name};
293 # the next possibility is they are
294 # a subtype of the built-in types,
295 # in which case this will DWIM in
296 # most cases. It is probably not
297 # 100% ideal though, but until I
298 # come up with a decent test case
299 # it will do for now.
300 foreach my $type (keys %TYPES) {
302 if $type_constraint->is_subtype_of($type);
306 # the reason the above will work has to
307 # do with the fact that custom subtypes
308 # are mostly used for validation of
309 # the guts of a type, and not for some
310 # weird structural thing which would
311 # need to be accomidated by the serializer.
312 # Of course, mst or phaylon will probably
313 # do something to throw this assumption
314 # totally out the door ;)
318 # if this method hasnt returned by now
319 # then we have no been able to find a
320 # type constraint handler to match
321 confess "Cannot handle type constraint (" . $type_constraint->name . ")";
332 MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects
336 No user serviceable parts inside. If you really want to know, read the source :)
358 =item B<expand_object>
360 =item B<collapse_object>
368 =item B<collapse_attribute>
370 =item B<collapse_attribute_value>
372 =item B<expand_attribute>
374 =item B<expand_attribute_value>
376 =item B<check_for_cycle_in_collapse>
378 =item B<check_for_cycle_in_expansion>
380 =item B<map_attributes>
384 =head2 Type Constraint Handlers
388 =item B<find_type_handler>
390 =item B<add_custom_type_handler>
392 =item B<remove_custom_type_handler>
406 All complex software has bugs lurking in it, and this module is no
407 exception. If you find a bug please either email me, or add the bug
412 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
414 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
416 =head1 COPYRIGHT AND LICENSE
418 Copyright 2007 by Infinity Interactive, Inc.
420 L<http://www.iinteractive.com>
422 This library is free software; you can redistribute it and/or modify
423 it under the same terms as Perl itself.