object proxying system
[scpubgit/Tak.git] / lib / Tak / ObjectService.pm
1 package Tak::ObjectService;
2
3 use overload ();
4 use Moo;
5
6 with 'Tak::Role::ObjectMangling';
7
8 has proxied => (is => 'ro', init_arg => undef, default => sub { {} });
9
10 sub inflate {
11   my ($self, $tag) = @_;
12   $self->proxied->{$tag};
13 }
14
15 sub deflate {
16   my ($self, $obj) = @_;
17   my $tag = overload::StrVal($obj);
18   $self->proxied->{$tag} = $obj;
19   return +{ __proxied_object__ => $tag };
20 }
21
22 sub handle_call_method {
23   my ($self, $context, $call) = @_;
24   my ($invocant, $method, @args) = @{$self->decode_objects($call)};
25   my @res;
26   eval {
27     if (!ref($invocant)) {
28       (my $file = $invocant) =~ s/::/\//g;
29       require "${file}.pm";
30     }
31     if ($context) {
32       @res = $invocant->$method(@args);
33     } elsif (defined $context) {
34       $res[0] = $invocant->$method(@args);
35     } else {
36       $invocant->$method(@args);
37     }
38     1;
39   } or return FAILURE => "$@";
40   return RESULT => $self->encode_objects(\@res);
41 }
42
43 sub handle_remove_object {
44   my ($self, $tag) = @_;
45   my $had = !!delete $self->proxied->{$tag};
46   return RESULT => $had;
47 }
48
49 1;