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 ...
29 my ( $self, %options ) = @_;
32 # mark the root object as seen ...
33 $self->seen->{$self->object} = undef;
35 $self->map_attributes('collapse_attribute', \%options);
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, $options) = @_;
58 $self->storage->{$attr->name} = $self->collapse_attribute_value($attr, $options) || 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, $options) = @_;
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, $options);
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, %$options);
191 my ( $obj, $options ) = @_;
192 # ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
193 # || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
195 || confess "Object does not have a &pack method, cannot collapse";
196 $obj->pack(%$options);
202 # These are boring ones, so they use the identity function ...
203 'Int' => { expand => sub { shift }, collapse => sub { shift } },
204 'Num' => { expand => sub { shift }, collapse => sub { shift } },
205 'Str' => { expand => sub { shift }, collapse => sub { shift } },
206 # These are the trickier ones, (see notes)
208 # Because we are nice guys, we will check
209 # your ArrayRef and/or HashRef one level
210 # down and inflate any objects we find.
211 # But this is where it ends, it is too
212 # expensive to try and do this any more
213 # recursively, when it is probably not
214 # nessecary in most of the use cases.
215 # However, if you need more then this, subtype
216 # and add a custom handler.
219 my ( $array, @args ) = @_;
220 foreach my $i (0 .. $#{$array}) {
221 next unless ref($array->[$i]) eq 'HASH'
222 && exists $array->[$i]->{$CLASS_MARKER};
223 $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
228 my ( $array, @args ) = @_;
230 # we need to make a copy cause
231 # otherwise it will affect the
232 # other real version.
235 ? $OBJECT_HANDLERS{collapse}->($_, @args)
242 my ( $hash, @args ) = @_;
243 foreach my $k (keys %$hash) {
244 next unless ref($hash->{$k}) eq 'HASH'
245 && exists $hash->{$k}->{$CLASS_MARKER};
246 $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
251 my ( $hash, @args ) = @_;
253 # we need to make a copy cause
254 # otherwise it will affect the
255 # other real version.
258 ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
259 : ($_ => $hash->{$_})
263 'Object' => \%OBJECT_HANDLERS,
265 # The sanity of enabling this feature by
266 # default is very questionable.
269 # expand => sub {}, # use eval ...
270 # collapse => sub {}, # use B::Deparse ...
274 sub add_custom_type_handler {
275 my ($class, $type_name, %handlers) = @_;
276 (exists $handlers{expand} && exists $handlers{collapse})
277 || confess "Custom type handlers need an expand *and* a collapse method";
278 $TYPES{$type_name} = \%handlers;
281 sub remove_custom_type_handler {
282 my ($class, $type_name) = @_;
283 delete $TYPES{$type_name} if exists $TYPES{$type_name};
286 sub find_type_handler {
287 my ($self, $type_constraint) = @_;
289 # this should handle most type usages
290 # since they they are usually just
291 # the standard set of built-ins
292 return $TYPES{$type_constraint->name}
293 if exists $TYPES{$type_constraint->name};
295 # the next possibility is they are
296 # a subtype of the built-in types,
297 # in which case this will DWIM in
298 # most cases. It is probably not
299 # 100% ideal though, but until I
300 # come up with a decent test case
301 # it will do for now.
302 foreach my $type (keys %TYPES) {
304 if $type_constraint->is_subtype_of($type);
308 # the reason the above will work has to
309 # do with the fact that custom subtypes
310 # are mostly used for validation of
311 # the guts of a type, and not for some
312 # weird structural thing which would
313 # need to be accomidated by the serializer.
314 # Of course, mst or phaylon will probably
315 # do something to throw this assumption
316 # totally out the door ;)
320 # if this method hasnt returned by now
321 # then we have no been able to find a
322 # type constraint handler to match
323 confess "Cannot handle type constraint (" . $type_constraint->name . ")";
334 MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects
338 No user serviceable parts inside. If you really want to know, read the source :)
360 =item B<expand_object>
362 =item B<collapse_object>
370 =item B<collapse_attribute>
372 =item B<collapse_attribute_value>
374 =item B<expand_attribute>
376 =item B<expand_attribute_value>
378 =item B<check_for_cycle_in_collapse>
380 =item B<check_for_cycle_in_expansion>
382 =item B<map_attributes>
386 =head2 Type Constraint Handlers
390 =item B<find_type_handler>
392 =item B<add_custom_type_handler>
394 =item B<remove_custom_type_handler>
408 All complex software has bugs lurking in it, and this module is no
409 exception. If you find a bug please either email me, or add the bug
414 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
416 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
418 =head1 COPYRIGHT AND LICENSE
420 Copyright 2007 by Infinity Interactive, Inc.
422 L<http://www.iinteractive.com>
424 This library is free software; you can redistribute it and/or modify
425 it under the same terms as Perl itself.