File Coverage

blib/lib/Bot/Backbone/Service.pm
Criterion Covered Total %
statement 29 41 70.7
branch n/a
condition n/a
subroutine 10 13 76.9
pod 3 3 100.0
total 42 57 73.6


line stmt bran cond sub pod time code
1             package Bot::Backbone::Service;
2             $Bot::Backbone::Service::VERSION = '0.142820';
3 1     1   388559 use v5.10;
  1         3  
  1         35  
4 1     1   3 use Moose();
  1         1  
  1         11  
5 1     1   346 use Bot::Backbone::DispatchSugar();
  1         3  
  1         26  
6 1     1   8 use Moose::Exporter;
  1         1  
  1         5  
7 1     1   73 use Moose::Util qw( ensure_all_roles );
  1         2  
  1         10  
8 1     1   244 use Class::Load;
  1         2  
  1         41  
9              
10 1     1   499 use Bot::Backbone::Meta::Class::Service;
  1         887  
  1         47  
11 1     1   553 use Bot::Backbone::Dispatcher;
  1         286  
  1         38  
12 1     1   621 use Bot::Backbone::Service::Role::Service;
  1         2  
  1         211  
13              
14             # ABSTRACT: Useful features for services
15              
16              
17             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 2     2 1 5651 shift;
25 2         9 Moose->init_meta(@_,
26             metaclass => 'Bot::Backbone::Meta::Class::Service',
27             );
28             };
29              
30              
31             sub with_bot_roles {
32 0     0 1   my ($meta, @roles) = @_;
33 0           Class::Load::load_class($_) for @roles;
34 0           $meta->add_bot_roles(@roles);
35             }
36              
37              
38             sub service_dispatcher($) {
39 0     0 1   my ($meta, $code) = @_;
40              
41 0           ensure_all_roles($meta->name, 'Bot::Backbone::Service::Role::Dispatch');
42              
43             $meta->dispatch_builder(sub {
44 0     0     my $dispatcher = Bot::Backbone::Dispatcher->new;
45             {
46 0           $meta->building_dispatcher($dispatcher);
  0            
47 0           $code->();
48 0           $meta->no_longer_building_dispatcher,
49             }
50 0           return $dispatcher;
51 0           });
52             }
53              
54              
55             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.142820
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