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 'Bool' => { expand => sub { shift }, collapse => sub { shift } },
208 # These are the trickier ones, (see notes)
210 # Because we are nice guys, we will check
211 # your ArrayRef and/or HashRef one level
212 # down and inflate any objects we find.
213 # But this is where it ends, it is too
214 # expensive to try and do this any more
215 # recursively, when it is probably not
216 # nessecary in most of the use cases.
217 # However, if you need more then this, subtype
218 # and add a custom handler.
221 my ( $array, @args ) = @_;
222 foreach my $i (0 .. $#{$array}) {
223 next unless ref($array->[$i]) eq 'HASH'
224 && exists $array->[$i]->{$CLASS_MARKER};
225 $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
230 my ( $array, @args ) = @_;
232 # we need to make a copy cause
233 # otherwise it will affect the
234 # other real version.
237 ? $OBJECT_HANDLERS{collapse}->($_, @args)
244 my ( $hash, @args ) = @_;
245 foreach my $k (keys %$hash) {
246 next unless ref($hash->{$k}) eq 'HASH'
247 && exists $hash->{$k}->{$CLASS_MARKER};
248 $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
253 my ( $hash, @args ) = @_;
255 # we need to make a copy cause
256 # otherwise it will affect the
257 # other real version.
260 ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
261 : ($_ => $hash->{$_})
265 'Object' => \%OBJECT_HANDLERS,
267 # The sanity of enabling this feature by
268 # default is very questionable.
271 # expand => sub {}, # use eval ...
272 # collapse => sub {}, # use B::Deparse ...
276 sub add_custom_type_handler {
277 my ($class, $type_name, %handlers) = @_;
278 (exists $handlers{expand} && exists $handlers{collapse})
279 || confess "Custom type handlers need an expand *and* a collapse method";
280 $TYPES{$type_name} = \%handlers;
283 sub remove_custom_type_handler {
284 my ($class, $type_name) = @_;
285 delete $TYPES{$type_name} if exists $TYPES{$type_name};
288 sub find_type_handler {
289 my ($self, $type_constraint) = @_;
291 # this should handle most type usages
292 # since they they are usually just
293 # the standard set of built-ins
294 return $TYPES{$type_constraint->name}
295 if exists $TYPES{$type_constraint->name};
297 # the next possibility is they are
298 # a subtype of the built-in types,
299 # in which case this will DWIM in
300 # most cases. It is probably not
301 # 100% ideal though, but until I
302 # come up with a decent test case
303 # it will do for now.
304 foreach my $type (keys %TYPES) {
306 if $type_constraint->is_subtype_of($type);
310 # the reason the above will work has to
311 # do with the fact that custom subtypes
312 # are mostly used for validation of
313 # the guts of a type, and not for some
314 # weird structural thing which would
315 # need to be accomidated by the serializer.
316 # Of course, mst or phaylon will probably
317 # do something to throw this assumption
318 # totally out the door ;)
322 # if this method hasnt returned by now
323 # then we have no been able to find a
324 # type constraint handler to match
325 confess "Cannot handle type constraint (" . $type_constraint->name . ")";
336 MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects
340 No user serviceable parts inside. If you really want to know, read the source :)
362 =item B<expand_object>
364 =item B<collapse_object>
372 =item B<collapse_attribute>
374 =item B<collapse_attribute_value>
376 =item B<expand_attribute>
378 =item B<expand_attribute_value>
380 =item B<check_for_cycle_in_collapse>
382 =item B<check_for_cycle_in_expansion>
384 =item B<map_attributes>
388 =head2 Type Constraint Handlers
392 =item B<find_type_handler>
394 =item B<add_custom_type_handler>
396 =item B<remove_custom_type_handler>
410 All complex software has bugs lurking in it, and this module is no
411 exception. If you find a bug please either email me, or add the bug
416 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
418 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
420 =head1 COPYRIGHT AND LICENSE
422 Copyright 2007 by Infinity Interactive, Inc.
424 L<http://www.iinteractive.com>
426 This library is free software; you can redistribute it and/or modify
427 it under the same terms as Perl itself.