From: Matt S Trout Date: Tue, 20 Dec 2011 08:35:10 +0000 (+0000) Subject: richer expose syntax X-Git-Tag: v0.001002~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=430a5f891d19a13cc07e99221ca3c87307ae8fa3;p=scpubgit%2FTak.git richer expose syntax --- diff --git a/lib/Tak/MetaService.pm b/lib/Tak/MetaService.pm index e80b7c5..7314db0 100644 --- a/lib/Tak/MetaService.pm +++ b/lib/Tak/MetaService.pm @@ -23,10 +23,28 @@ sub handle_register { my ($self, $name, $class, %args) = @_; (my $file = $class) =~ s/::/\//g; require "${file}.pm"; - my $router = $self->router; if (my $expose = delete $args{expose}) { - foreach my $name (keys %$expose) { - if (my ($svc, @rest) = @{$expose->{$name}}) { + %args = (%args, %{$self->_construct_exposed_clients($expose)}); + } + my $new = $class->new(\%args); + $self->router->register($name => $new); + return "Registered ${name}"; +} + +sub _construct_exposed_clients { + my ($self, $expose) = @_; + my $router = $self->router; + my %client; + foreach my $name (keys %$expose) { + local $_ = $expose->{$name}; + if (ref eq 'HASH') { + $client{$name} = Tak::Client->new( + service => Tak::Router->new( + services => $self->_construct_exposed_clients($_) + ) + ); + } elsif (ref eq 'ARRAY') { + if (my ($svc, @rest) = @$_) { die "router has no service ${svc}" unless my $service = $router->services->{$svc}; my $client_class = ( @@ -34,16 +52,16 @@ sub handle_register { ? 'Tak::WeakClient' : 'Tak::Client' ); - $args{$name} = $client_class->new(service => $service) - ->curry(@rest); + $client{$name} = $client_class->new(service => $service) + ->curry(@rest); } else { - $args{$name} = Tak::WeakClient->new(service => $router); + $client{$name} = Tak::WeakClient->new(service => $router); } + } else { + die "expose key ${name} was ".ref; } } - my $new = $class->new(\%args); - $router->register($name => $new); - return "Registered ${name}"; + \%client; } 1;