2 package MooseX::Storage::Engine;
6 our $AUTHORITY = 'cpan:STEVAN';
8 # the class marker when
9 # serializing an object.
10 our $CLASS_MARKER = '__CLASS__';
24 has 'object' => (is => 'rw', isa => 'Object');
25 has 'class' => (is => 'rw', isa => 'Str');
27 ## this is the API used by other modules ...
30 my ( $self, %options ) = @_;
33 # mark the root object as seen ...
34 $self->seen->{$self->object} = undef;
36 $self->map_attributes('collapse_attribute', \%options);
37 $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;
38 return $self->storage;
42 my ($self, $data, %options) = @_;
44 $options{check_version} = 1 unless exists $options{check_version};
45 $options{check_authority} = 1 unless exists $options{check_authority};
48 # mark the root object as seen ...
49 $self->seen->{$data} = undef;
51 $self->map_attributes('expand_attribute', $data, \%options);
52 return $self->storage;
55 ## this is the internal API ...
57 sub collapse_attribute {
58 my ($self, $attr, $options) = @_;
59 $self->storage->{$attr->name} = $self->collapse_attribute_value($attr, $options) || return;
62 sub expand_attribute {
63 my ($self, $attr, $data, $options) = @_;
64 $self->storage->{$attr->name} = $self->expand_attribute_value($attr, $data->{$attr->name}, $options) || return;
67 sub collapse_attribute_value {
68 my ($self, $attr, $options) = @_;
69 my $value = $attr->get_value($self->object);
72 # this might not be enough, we might
73 # need to make it possible for the
74 # cycle checker to return the value
75 $self->check_for_cycle_in_collapse($attr, $value)
78 if (defined $value && $attr->has_type_constraint) {
79 my $type_converter = $self->find_type_handler($attr->type_constraint);
80 (defined $type_converter)
81 || confess "Cannot convert " . $attr->type_constraint->name;
82 $value = $type_converter->{collapse}->($value, $options);
87 sub expand_attribute_value {
88 my ($self, $attr, $value, $options) = @_;
91 # (see comment in method above ^^)
92 $self->check_for_cycle_in_expansion($attr, $value)
95 if (defined $value && $attr->has_type_constraint) {
96 my $type_converter = $self->find_type_handler($attr->type_constraint);
97 $value = $type_converter->{expand}->($value, $options);
103 # possibly these two methods will
104 # be used by a cycle supporting
105 # engine. However, I am not sure
106 # if I can make a cycle one work
109 sub check_for_cycle_in_collapse {
110 my ($self, $attr, $value) = @_;
111 (!exists $self->seen->{$value})
112 || confess "Basic Engine does not support cycles in class("
113 . ($attr->associated_class->name) . ").attr("
114 . ($attr->name) . ") with $value";
115 $self->seen->{$value} = undef;
118 sub check_for_cycle_in_expansion {
119 my ($self, $attr, $value) = @_;
120 (!exists $self->seen->{$value})
121 || confess "Basic Engine does not support cycles in class("
122 . ($attr->associated_class->name) . ").attr("
123 . ($attr->name) . ") with $value";
124 $self->seen->{$value} = undef;
130 my ($self, $method_name, @args) = @_;
132 $self->$method_name($_, @args)
134 # Skip our special skip attribute :)
135 !$_->isa('MooseX::Storage::Meta::Attribute::DoNotSerialize')
136 } ($self->object || $self->class)->meta->compute_all_applicable_attributes;
139 ## ------------------------------------------------------------------
140 ## This is all the type handler stuff, it is in a state of flux
141 ## right now, so this may change, or it may just continue to be
142 ## improved upon. Comments and suggestions are welcomed.
143 ## ------------------------------------------------------------------
146 # these are needed by the
147 # ArrayRef and HashRef handlers
148 # below, so I need easy access
149 my %OBJECT_HANDLERS = (
151 my ($data, $options) = @_;
152 (exists $data->{$CLASS_MARKER})
153 || confess "Serialized item has no class marker";
154 # check the class more thoroughly here ...
155 my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER});
156 my $meta = eval { $class->meta };
157 confess "Class ($class) is not loaded, cannot unpack" if $@;
159 if ($options->{check_version}) {
160 my $meta_version = $meta->version;
161 if (defined $meta_version && $version) {
162 if ($options->{check_version} eq 'allow_less_than') {
163 ($meta_version <= $version)
164 || confess "Class ($class) versions is not less than currently available."
165 . " got=($version) available=($meta_version)";
167 elsif ($options->{check_version} eq 'allow_greater_than') {
168 ($meta->version >= $version)
169 || confess "Class ($class) versions is not greater than currently available."
170 . " got=($version) available=($meta_version)";
173 ($meta->version == $version)
174 || confess "Class ($class) versions don't match."
175 . " got=($version) available=($meta_version)";
180 if ($options->{check_authority}) {
181 my $meta_authority = $meta->authority;
182 ($meta->authority eq $authority)
183 || confess "Class ($class) authorities don't match."
184 . " got=($authority) available=($meta_authority)"
185 if defined $meta_authority && defined $authority;
189 $class->unpack($data, %$options);
192 my ( $obj, $options ) = @_;
193 # ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
194 # || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
196 || confess "Object does not have a &pack method, cannot collapse";
197 $obj->pack(%$options);
203 # These are boring ones, so they use the identity function ...
204 'Int' => { expand => sub { shift }, collapse => sub { shift } },
205 'Num' => { expand => sub { shift }, collapse => sub { shift } },
206 'Str' => { expand => sub { shift }, collapse => sub { shift } },
207 # These are the trickier ones, (see notes)
209 # Because we are nice guys, we will check
210 # your ArrayRef and/or HashRef one level
211 # down and inflate any objects we find.
212 # But this is where it ends, it is too
213 # expensive to try and do this any more
214 # recursively, when it is probably not
215 # nessecary in most of the use cases.
216 # However, if you need more then this, subtype
217 # and add a custom handler.
220 my ( $array, @args ) = @_;
221 foreach my $i (0 .. $#{$array}) {
222 next unless ref($array->[$i]) eq 'HASH'
223 && exists $array->[$i]->{$CLASS_MARKER};
224 $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
229 my ( $array, @args ) = @_;
231 # we need to make a copy cause
232 # otherwise it will affect the
233 # other real version.
236 ? $OBJECT_HANDLERS{collapse}->($_, @args)
243 my ( $hash, @args ) = @_;
244 foreach my $k (keys %$hash) {
245 next unless ref($hash->{$k}) eq 'HASH'
246 && exists $hash->{$k}->{$CLASS_MARKER};
247 $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
252 my ( $hash, @args ) = @_;
254 # we need to make a copy cause
255 # otherwise it will affect the
256 # other real version.
259 ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
260 : ($_ => $hash->{$_})
264 'Object' => \%OBJECT_HANDLERS,
266 # The sanity of enabling this feature by
267 # default is very questionable.
270 # expand => sub {}, # use eval ...
271 # collapse => sub {}, # use B::Deparse ...
275 sub add_custom_type_handler {
276 my ($class, $type_name, %handlers) = @_;
277 (exists $handlers{expand} && exists $handlers{collapse})
278 || confess "Custom type handlers need an expand *and* a collapse method";
279 $TYPES{$type_name} = \%handlers;
282 sub remove_custom_type_handler {
283 my ($class, $type_name) = @_;
284 delete $TYPES{$type_name} if exists $TYPES{$type_name};
287 sub find_type_handler {
288 my ($self, $type_constraint) = @_;
290 # this should handle most type usages
291 # since they they are usually just
292 # the standard set of built-ins
293 return $TYPES{$type_constraint->name}
294 if exists $TYPES{$type_constraint->name};
296 # the next possibility is they are
297 # a subtype of the built-in types,
298 # in which case this will DWIM in
299 # most cases. It is probably not
300 # 100% ideal though, but until I
301 # come up with a decent test case
302 # it will do for now.
303 foreach my $type (keys %TYPES) {
305 if $type_constraint->is_subtype_of($type);
309 # the reason the above will work has to
310 # do with the fact that custom subtypes
311 # are mostly used for validation of
312 # the guts of a type, and not for some
313 # weird structural thing which would
314 # need to be accomidated by the serializer.
315 # Of course, mst or phaylon will probably
316 # do something to throw this assumption
317 # totally out the door ;)
321 # if this method hasnt returned by now
322 # then we have no been able to find a
323 # type constraint handler to match
324 confess "Cannot handle type constraint (" . $type_constraint->name . ")";
335 MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects
339 No user serviceable parts inside. If you really want to know, read the source :)
361 =item B<expand_object>
363 =item B<collapse_object>
371 =item B<collapse_attribute>
373 =item B<collapse_attribute_value>
375 =item B<expand_attribute>
377 =item B<expand_attribute_value>
379 =item B<check_for_cycle_in_collapse>
381 =item B<check_for_cycle_in_expansion>
383 =item B<map_attributes>
387 =head2 Type Constraint Handlers
391 =item B<find_type_handler>
393 =item B<add_custom_type_handler>
395 =item B<remove_custom_type_handler>
409 All complex software has bugs lurking in it, and this module is no
410 exception. If you find a bug please either email me, or add the bug
415 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
417 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
419 =head1 COPYRIGHT AND LICENSE
421 Copyright 2007 by Infinity Interactive, Inc.
423 L<http://www.iinteractive.com>
425 This library is free software; you can redistribute it and/or modify
426 it under the same terms as Perl itself.