Backbone/Service.pm

filter: Statementstmt 93.0% 40 / 43 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 93.2% 55 / 59 CC 2 SCAR 5.4
CC
2
Cov
93%
with_bot_roles
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.7
« Backbone/Bot.pm ↑ Summary Backbone/Types.pm »
1 package Bot::Backbone::Service;
2 4 $Bot::Backbone::Service::VERSION = '0.142220';
3 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Bot::Backbone::DispatchSugar();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Moose::Util qw( ensure_all_roles );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Bot::Backbone::Meta::Class::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Bot::Backbone::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Bot::Backbone::Service::Role::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Useful features for services
15  
16  
17 4 Moose::Exporter->setup_import_methods(
18 with_meta => [ qw( service_dispatcher with_bot_roles ) ],
19 also => [ qw( Moose Bot::Backbone::DispatchSugar ) ],
20 );
21  
22  
23 sub init_meta {
24 6 ▶ shift;
Subroutine init_meta: called CC=1 SCAR=0.0
Pod documented
25 6 Moose->init_meta(@_,
26 metaclass => 'Bot::Backbone::Meta::Class::Service',
27 );
28 };
29  
30  
31 sub with_bot_roles {
32 0 ▶ my ($meta, @roles) = @_;
Subroutine with_bot_roles: not called CC=2 SCAR=17.9
Pod documented
33 0 Class::Load::load_class($_) for @roles;
34 0 $meta->add_bot_roles(@roles);
35 }
36  
37  
38 sub service_dispatcher($) {
39 2 ▶ my ($meta, $code) = @_;
Subroutine service_dispatcher: called CC=1 SCAR=0.0
Pod documented
40  
41 2 ensure_all_roles($meta->name, 'Bot::Backbone::Service::Role::Dispatch');
42  
43 $meta->dispatch_builder(sub {
44 3 ▶ my $dispatcher = Bot::Backbone::Dispatcher->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
45 {
46 3 $meta->building_dispatcher($dispatcher);
47 3 $code->();
48 3 $meta->no_longer_building_dispatcher,
49 }
50 3 return $dispatcher;
51 2 });
52 }
53  
54  
55 4 1;
56  
57 __END__
58  
59 =pod
60  
61 =encoding UTF-8
62  
63 =head1 NAME
64  
65 Bot::Backbone::Service - Useful features for services
66  
67 =head1 VERSION
68  
69 version 0.142220
70  
71 =head1 SYNOPSIS
72  
73 package MyBot::Service::Echo;
74 use v5.14; # because newer Perl is cooler than older Perl
75 use Bot::Backbone::Service;
76  
77 with qw(
78 Bot::Backbone::Service::Role::Service
79 Bot::Backbone::Service::Role::Responder
80 );
81  
82 # Instead of Bot::Backbone::Service::Role::Responder, you may prefer to
83 # apply the Bot::Backbone::Service::Role::ChatConsumer role instead. It
84 # really depends on if this module will be used across multiple chats or
85 # needs to be tied to a specific chat.
86  
87 service_dispatcher as {
88 command '!echo' => given_parameters {
89 parameter thing => ( match => qr/.+/ );
90 } respond_by_method 'echo_back';
91 };
92  
93 sub echo_back {
94 my ($self, $message) = @_;
95 return $message->parameters->{thing};
96 }
97  
98 __PACKAGE__->meta->make_immutable; # very good idea
99  
100 =head1 DESCRIPTION
101  
102 This is a Moose-replacement for bot backbone services. It provides a similar set of features to a service class as are provided to bot classes by L<Bot::Backbone>.
103  
104 =head1 SUBROUTINES
105  
106 =head2 init_meta
107  
108 Setup the bot package by applying the L<Bot::Backbone::Service::Role::Service> role to the class.
109  
110 =head1 SETUP ROUTINES
111  
112 =head2 with_bot_roles
113  
114 with_bot_roles ...;
115  
116 Similar to C<with> provided by L<Moose>, this defines a list of roles that should be applied to the bot that uses this service.
117  
118 =head2 service_dispatcher
119  
120 service_dispatcher ...;
121  
122 Setup the default dispatcher for this service. Use of this method will cause the L<Bot::Backbone::Service::Role::Dispatch> role to be applied to the class.
123  
124 =head1 DISPATCHER PREDICATES
125  
126 This exports all the same dispatcher predicates as L<Bot::Backbone>.
127  
128 =over
129  
130 =item *
131  
132 C<redispatch_to>
133  
134 =item *
135  
136 C<command>
137  
138 =item *
139  
140 C<not_command>
141  
142 =item *
143  
144 C<given_parameters> (and C<parameter>)
145  
146 =item *
147  
148 C<to_me>
149  
150 =item *
151  
152 C<not_to_me>
153  
154 =item *
155  
156 C<shouted>
157  
158 =item *
159  
160 C<spoken>
161  
162 =item *
163  
164 C<whispered>
165  
166 =item *
167  
168 C<also>
169  
170 =back
171  
172 =head1 RUN MODE OPERATIONS
173  
174 This exports all the same run mode operations as L<Bot::Backbone>.
175  
176 =over
177  
178 =item *
179  
180 C<as>
181  
182 =item *
183  
184 C<respond>. This run mode operation will be passed the service object as the first argument, rather than that bot object.
185  
186 =item *
187  
188 C<respond_with_method>. As stated for C<respond>, the first argument is the service object. The method is also a method defined within the current service package rather than the bot.
189  
190 =item *
191  
192 C<respond_with_bot_method>. This is similar to C<respond_with_method>, but instead of calling a method within the service, it will call a method directly on the bot to which the service has been added.
193  
194 =item *
195  
196 C<run_this>. This run mode operation will be passed the service object as the first argument, rather than that bot object.
197  
198 =item *
199  
200 C<run_this_method>. As stated for C<respond>, the first argument is the service object. The method is also a method defined within the current service package rather than the bot.
201  
202 =item *
203  
204 C<run_this_bot_method>. This is similar to C<run_this_method>, but results in a call to a method on the bot object rather than on the service.
205  
206 =back
207  
208 =head1 AUTHOR
209  
210 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
211  
212 =head1 COPYRIGHT AND LICENSE
213  
214 This software is copyright (c) 2014 by Qubling Software LLC.
215  
216 This is free software; you can redistribute it and/or modify it under
217 the same terms as the Perl 5 programming language system itself.
218  
219 =cut
« Backbone/Bot.pm ↑ Summary Backbone/Types.pm »