File Coverage

blib/lib/MojoX/JSON/RPC/Service/AutoRegister.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 4 4 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1             package MojoX::JSON::RPC::Service::AutoRegister;
2 4     4   1013293 use Mojo::Base 'MojoX::JSON::RPC::Service';
  4         9252  
  4         23  
3             our $VERSION = 0.001;
4              
5             =head1 NAME
6              
7             MojoX::JSON::RPC::Service::AutoRegister - Base class for RPC Services
8              
9             =head1 DESCRIPTION
10              
11             This object represent a base class for RPC Services.
12             It only ovverides the C to inject C<'with_mojo_tx'=1>, C<'with_svc_obj'=1> and C<'with_self'=1> options by default.
13             For more information on how services work, have a look at
14             L.
15              
16             Every function that starts with C it's automatically registered as an
17             rpc service, this means that on your service file you must only add
18              
19             __PACKAGE__->register_rpc;
20              
21             at the bottom of the code.
22             You can also defines your suffix or your regex to match the functions to being automatically registered.
23              
24             =head1 METHODS
25              
26             Inherits all methods from L and adds the following new ones:
27              
28             =head2 register_rpc
29              
30             witouth arguments, register all the methods of the class that starts with "rpc_" as a RPC services
31              
32             =head2 register_rpc_suffix
33              
34             __PACKAGE__->register_rpc_suffix("somesuffix");
35              
36             Accept an argument, the suffix name. Register all the methods of the class that starts with the given suffix as a RPC services (e.g. somesuffix_edit, somesuffix_lay )
37              
38             =head2 register_rpc_regex
39              
40             __PACKAGE__->register_rpc_regex(qr//);
41              
42             Accept an argument, a regex. Register all the methods of the class that matches the given regex as a RPC services
43              
44             =head1 AUTHOR
45              
46             mudler Emudler@dark-lab.netE, vytas Evytas@cpan.orgE
47              
48             =head1 COPYRIGHT
49              
50             Copyright 2014- mudler, vytas
51              
52             =head1 LICENSE
53              
54             This library is free software; you can redistribute it and/or modify
55             it under the same terms as Perl itself.
56              
57             =head1 SEE ALSO
58              
59             L
60              
61             =cut
62              
63             sub new {
64 4     4 1 116608 my ( $self, @params ) = @_;
65              
66 4         40 $self = $self->SUPER::new(@params);
67              
68 4         106 foreach my $method ( keys %{ $self->{'_rpcs'} } ) {
  4         41  
69 11         25 $self->{'_rpcs'}->{$method}->{'with_mojo_tx'} = 1;
70 11         19 $self->{'_rpcs'}->{$method}->{'with_svc_obj'} = 1;
71 11         21 $self->{'_rpcs'}->{$method}->{'with_self'} = 1;
72             }
73              
74 4         55 return $self;
75             }
76              
77             sub register_rpc {
78 1     1 1 737 my $caller_package_name = caller;
79              
80 1         12 MojoX::JSON::RPC::Service::AutoRegister->register_rpc_regex(
81             qr/^ rpc _ /x, $caller_package_name, );
82             }
83              
84             sub register_rpc_suffix {
85 1     1 1 107 my ( undef, $suffix ) = @_;
86              
87 1         3 my $caller_package_name = caller;
88              
89 1         26 MojoX::JSON::RPC::Service::AutoRegister->register_rpc_regex(
90             qr/^ $suffix _ /x,
91             $caller_package_name, );
92             }
93              
94             sub register_rpc_regex {
95 3     3 1 125 my ( undef, $regex, $package ) = @_;
96 3 100       11 $package = caller if ( !defined($package) );
97              
98 3         6 my @methods = ();
99 3         155 my $symbols = { eval( '%' . $package . '::' ) };
100              
101 3         29 foreach my $entry ( keys %{$symbols} ) {
  3         19  
102              
103             # this allows functions that match the regex to be automatically
104             # exported as rpc public services
105 53 100 100     574 if ( defined( $package->can($entry) ) && ( $entry =~ $regex ) ) {
106 3         8 push( @methods, $entry );
107             }
108             }
109              
110 3         24 $package->register_rpc_method_names(@methods);
111             }
112              
113             1;