lib/Myriad/Service/Attributes.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« lib/Myriad/Registry.pm ↑ Summary lib/Myriad/Service/Implementation.pm »
1 package Myriad::Service::Attributes;
2  
3 1 ▶use Myriad::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = '1.001'; # VERSION
6 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Myriad::Service::Attributes - microservice coördination
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =cut
19  
20 =head1 Attributes
21  
22 Each of these is an attribute that can be applied to a method.
23  
24 Note that this class is just a simple passthrough to L<Myriad::Registry>,
25 which does all the real work.
26  
27 =cut
28  
29 0 ▶use Myriad::Registry;
Subroutine BEGIN: called CC=1 SCAR=2.6
30  
31 use Sub::Util ();
32  
33 our %KNOWN_ATTRIBUTES = (
34 RPC => 'rpc',
35 Batch => 'batch',
36 Emitter => 'emitter',
37 Receiver => 'receiver',
38 );
39  
40 =head1 METHODS
41  
42 =head2 apply_attributes
43  
44 Due to L<Attribute::Handlers> limitations at runtime, we need to pick
45 up attributes ourselves.
46  
47 =cut
48  
49 sub apply_attributes {
50 my ($class, %args) = @_;
51 my $pkg = $args{class};
52 my ($method) = Sub::Util::subname($args{code}) =~ /::([^:]+)$/;
53 for my $attr ($args{attributes}->@*) {
54 my ($type, $args) = $attr =~ m{^([a-z]+)(.*$)}si;
55 # Nasty, but functional for now - this will likely be replaced by
56 # an m//gc parser later with a restricted set of options.
57 $args = eval "+{ $args }" // die 'invalid attribute parameters: ' . $@ if length $args;
58  
59 $log->tracef('Applying %s attribute to %s::%s with args (%s)', $type, $pkg, $method, $args);
60 my $handler = $KNOWN_ATTRIBUTES{$type}
61 or die 'unknown attribute ' . $type;
62 $class->$handler(
63 $pkg,
64 $method,
65 $args{code},
66 $args
67 );
68 }
69 return;
70 }
71  
72 =head2 RPC
73  
74 Mark this async method as a callable RPC method.
75  
76 async method example_rpc : RPC (%args) {
77 return \%args;
78 }
79  
80 This will cause the method to be registered in L<Myriad::Registry/add_rpc>.
81  
82 =cut
83  
84 sub rpc {
85 my ($class, $pkg, $method, $code, $args) = @_;
86 require Myriad;
87 $Myriad::REGISTRY->add_rpc(
88 $pkg,
89 $method,
90 $code,
91 $args
92 );
93 }
94  
95 =head2 Batch
96  
97 Mark this as an async method which should be called repeatedly to generate
98 arrayref batches of data.
99  
100 has $id = 0;
101 async method example_batch : Batch {
102 return [ ++$id ];
103 }
104  
105 =cut
106  
107 sub batch {
108 my ($class, $pkg, $method, $code, $args) = @_;
109 $Myriad::REGISTRY->add_batch(
110 $pkg,
111 $method,
112 $code,
113 $args,
114 );
115 }
116  
117 =head2 Emitter
118  
119 Indicates a method which should be called on startup, which given a
120 L<Ryu::Sink> will emit events to that sink until it's done.
121  
122 =cut
123  
124 sub emitter {
125 my ($class, $pkg, $method, $code, $args) = @_;
126 $Myriad::REGISTRY->add_emitter(
127 $pkg,
128 $method,
129 $code,
130 $args,
131 );
132 }
133  
134 =head2 Receiver
135  
136 Indicates a method which should be called on startup and passed a
137 L<Ryu::Source>. Events will be emitted to that source until termination.
138  
139 =cut
140  
141 sub receiver {
142 my ($class, $pkg, $method, $code, $args) = @_;
143 $Myriad::REGISTRY->add_receiver(
144 $pkg,
145 $method,
146 $code,
147 $args,
148 );
149 }
150  
151 1;
152  
153 =head1 AUTHOR
154  
155 Deriv Group Services Ltd. C<< DERIV@cpan.org >>.
156  
157 See L<Myriad/CONTRIBUTORS> for full details.
158  
159 =head1 LICENSE
160  
161 Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself.
162  
« lib/Myriad/Registry.pm ↑ Summary lib/Myriad/Service/Implementation.pm »