| 1 | package Bot::Backbone::Service::Role::ChatConsumer; | ||
| 2 | $Bot::Backbone::Service::Role::ChatConsumer::VERSION = '0.161950'; | ||
| 3 | use v5.10; | ||
| 4 | use Moose::Role; | ||
| 5 | |||
| 6 | with 'Bot::Backbone::Service::Role::SendPolicy'; | ||
| 7 | |||
| 8 | # ABSTRACT: Role for services that listen for chat messages | ||
| 9 | |||
| 10 | |||
| 11 | has chat_name => ( | ||
| 12 | is => 'ro', | ||
| 13 | isa => 'Str', | ||
| 14 | required => 1, | ||
| 15 | init_arg => 'chat', | ||
| 16 | predicate => 'has_chat', | ||
| 17 | ); | ||
| 18 | |||
| 19 | |||
| 20 | has chat => ( | ||
| 21 | is => 'ro', | ||
| 22 | does => 'Bot::Backbone::Service::Role::Chat', | ||
| 23 | init_arg => undef, | ||
| 24 | lazy_build => 1, | ||
| 25 | weak_ref => 1, | ||
| 26 | |||
| 27 | # lazy_build implies (predicate => has_chat) | ||
| 28 | predicate => 'has_setup_the_chat', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # XXX Don't delegate these two. Delegation and -excludes don't seem to | ||
| 32 | # cooperate very well. | ||
| 33 | sub send_message { shift->chat->send_message(@_) } | ||
| 34 | sub send_reply { shift->chat->send_reply(@_) } | ||
| 35 | |||
| 36 | sub _build_chat { | ||
| 37 | my $self = shift; | ||
| 38 | my $chat = $self->bot->services->{ $self->chat_name }; | ||
| 39 | |||
| 40 | die "no such chat as ", $self->chat_name, "\n" | ||
| 41 | unless defined $chat; | ||
| 42 | |||
| 43 | return $chat; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | requires 'receive_message'; | ||
| 48 | |||
| 49 | |||
| 50 | before initialize => sub { | ||
| 51 | my $self = shift; | ||
| 52 | my $chat = $self->chat if $self->has_chat; | ||
| 53 | $chat->register_chat_consumer($self); | ||
| 54 | }; | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ |