File Coverage

blib/lib/Tak/MetaService.pm
Criterion Covered Total %
statement 9 37 24.3
branch 0 14 0.0
condition n/a
subroutine 3 7 42.8
pod 0 3 0.0
total 12 61 19.6


line stmt bran cond sub pod time code
1             package Tak::MetaService;
2              
3 1     1   445 use Tak::WeakClient;
  1         3  
  1         34  
4 1     1   6 use Log::Contextual qw(:log);
  1         2  
  1         7  
5 1     1   1304 use Moo;
  1         3  
  1         5  
6              
7             with 'Tak::Role::Service';
8              
9             has router => (is => 'ro', required => 1, weak_ref => 1);
10              
11             sub handle_pid {
12 0     0 0   return $$;
13             }
14              
15             sub handle_ensure {
16 0     0 0   my $self = shift;
17 0           my ($name) = @_;
18 0 0         return "Already have ${name}" if $self->router->services->{$name};
19 0           $self->handle_register(@_);
20             }
21              
22             sub handle_register {
23 0     0 0   my ($self, $name, $class, %args) = @_;
24 0           (my $file = $class) =~ s/::/\//g;
25 0           require "${file}.pm";
26 0 0         if (my $expose = delete $args{expose}) {
27 0           %args = (%args, %{$self->_construct_exposed_clients($expose)});
  0            
28             }
29 0           my $new = $class->new(\%args);
30 0           $self->router->register($name => $new);
31 0           return "Registered ${name}";
32             }
33              
34             sub _construct_exposed_clients {
35 0     0     my ($self, $expose) = @_;
36 0           my $router = $self->router;
37 0           my %client;
38 0           foreach my $name (keys %$expose) {
39 0           local $_ = $expose->{$name};
40 0 0         if (ref eq 'HASH') {
    0          
41 0           $client{$name} = Tak::Client->new(
42             service => Tak::Router->new(
43             services => $self->_construct_exposed_clients($_)
44             )
45             );
46             } elsif (ref eq 'ARRAY') {
47 0 0         if (my ($svc, @rest) = @$_) {
48             die "router has no service ${svc}"
49 0 0         unless my $service = $router->services->{$svc};
50             my $client_class = (
51 0 0         Scalar::Util::isweak($router->services->{$svc})
52             ? 'Tak::WeakClient'
53             : 'Tak::Client'
54             );
55 0           $client{$name} = $client_class->new(service => $service)
56             ->curry(@rest);
57             } else {
58 0           $client{$name} = Tak::WeakClient->new(service => $router);
59             }
60             } else {
61 0           die "expose key ${name} was ".ref;
62             }
63             }
64 0           \%client;
65             }
66              
67             1;