File Coverage

blib/lib/Bot/Backbone/Service.pm
Criterion Covered Total %
statement 37 40 92.5
branch n/a
condition n/a
subroutine 12 13 92.3
pod 3 3 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package Bot::Backbone::Service;
2             $Bot::Backbone::Service::VERSION = '0.161950';
3 4     4   343027 use v5.10;
  4         12  
4 4     4   15 use Moose();
  4         4  
  4         41  
5 4     4   363 use Bot::Backbone::DispatchSugar();
  4         9  
  4         64  
6 4     4   16 use Moose::Exporter;
  4         4  
  4         28  
7 4     4   161 use Moose::Util qw( ensure_all_roles );
  4         7  
  4         35  
8 4     4   973 use Class::Load;
  4         5  
  4         162  
9              
10 4     4   1637 use Bot::Backbone::Meta::Class::Service;
  4         4255  
  4         189  
11 4     4   495 use Bot::Backbone::Dispatcher;
  4         278  
  4         130  
12 4     4   2591 use Bot::Backbone::Service::Role::Service;
  4         9  
  4         861  
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 6     6 1 5539 shift;
25 6         24 Moose->init_meta(@_,
26             metaclass => 'Bot::Backbone::Meta::Class::Service',
27             );
28             };
29              
30              
31             sub with_bot_roles {
32 0     0 1 0 my ($meta, @roles) = @_;
33 0         0 Class::Load::load_class($_) for @roles;
34 0         0 $meta->add_bot_roles(@roles);
35             }
36              
37              
38             sub service_dispatcher($) {
39 2     2 1 28 my ($meta, $code) = @_;
40              
41 2         12 ensure_all_roles($meta->name, 'Bot::Backbone::Service::Role::Dispatch');
42              
43             $meta->dispatch_builder(sub {
44 3     3   70 my $dispatcher = Bot::Backbone::Dispatcher->new;
45             {
46 3         3 $meta->building_dispatcher($dispatcher);
  3         87  
47 3         9 $code->();
48 3         103 $meta->no_longer_building_dispatcher,
49             }
50 3         74 return $dispatcher;
51 2         3366 });
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.161950
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) 2016 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