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