| 1 | package Bot::Backbone::Bot; | ||||||||||
| 2 | 4 | $Bot::Backbone::Bot::VERSION = '0.161950'; | |||||||||
| 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 | |||||||||||
| 6 | 4 | ▶ | use Bot::Backbone::Types qw( EventLoop ServiceList ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use POE qw( Loop::AnyEvent ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # ABSTRACT: Provides backbone services to your bot | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | 4 | has event_loop => ( | |||||||||
| 13 | is => 'ro', | ||||||||||
| 14 | isa => EventLoop, | ||||||||||
| 15 | required => 1, | ||||||||||
| 16 | default => 'POE::Kernel', | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | has services => ( | ||||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | isa => ServiceList, | ||||||||||
| 23 | required => 1, | ||||||||||
| 24 | 4 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 4 | traits => [ 'Hash' ], | |||||||||
| 26 | handles => { | ||||||||||
| 27 | add_service => 'set', | ||||||||||
| 28 | service_names => 'keys', | ||||||||||
| 29 | list_services => 'values', | ||||||||||
| 30 | destroy_services => 'clear', | ||||||||||
| 31 | has_service => 'defined', | ||||||||||
| 32 | get_service => 'get', | ||||||||||
| 33 | }, | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | has initialized_services => ( | ||||||||||
| 38 | is => 'ro', | ||||||||||
| 39 | isa => 'HashRef[Bool]', | ||||||||||
| 40 | required => 1, | ||||||||||
| 41 | 4 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 4 | ); | |||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub bot { $_[0] } | ||||||||
|
Subroutine
bot: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 10 | ▶ | sub _ordered_services { | ||||||||
|
Subroutine
_ordered_services: called CC=6 SCAR=21.5
| |||||||||||
| 49 | 10 | my $self = shift; | |||||||||
| 50 | |||||||||||
| 51 | 10 | my %forest; | |||||||||
| 52 | 10 | for my $pair ($self->meta->services_kv) { | |||||||||
| 53 | 24 | my ($name, $config) = @$pair; | |||||||||
| 54 | |||||||||||
| 55 | 24 | ▶ | if ($config->{service}->does('Bot::Backbone::Service::Role::ChatConsumer')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | $forest{ $name } = $config->{chat}; | |||||||||
| 57 | } | ||||||||||
| 58 | else { | ||||||||||
| 59 | 24 | $forest{ $name } = undef; | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 10 | my @names; | |||||||||
| 64 | 10 | for my $name (keys %forest) { | |||||||||
| 65 | 24 | ▶ | if (defined $forest{ $name }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | my $depth = 1; | |||||||||
| 67 | 0 | my $next_name = $forest{ $name }; | |||||||||
| 68 | 0 | ▶ | while (defined($next_name = $forest{ $next_name })) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | $depth++; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | $forest{ $name } = $depth; | |||||||||
| 73 | } | ||||||||||
| 74 | else { | ||||||||||
| 75 | 24 | $forest{ $name } = 0; | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 10 | return sort { $forest{ $a } <=> $forest{ $b } } keys %forest; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 5 | ▶ | sub construct_services { | ||||||||
|
Subroutine
construct_services: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 83 | 5 | my $self = shift; | |||||||||
| 84 | |||||||||||
| 85 | 5 | my $my_name = $self->meta->name; | |||||||||
| 86 | |||||||||||
| 87 | 5 | my @names = $self->_ordered_services; | |||||||||
| 88 | |||||||||||
| 89 | 5 | for my $name ($self->_ordered_services) { | |||||||||
| 90 | 12 | my $service_config = $self->meta->services->{$name}; | |||||||||
| 91 | 12 | ▶ | next if defined $self->services->{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | |||||||||||
| 93 | 9 | my $class_name = $service_config->{service}; | |||||||||
| 94 | 9 | my $service = $class_name->new( | |||||||||
| 95 | %$service_config, | ||||||||||
| 96 | name => $name, | ||||||||||
| 97 | bot => $self, | ||||||||||
| 98 | ); | ||||||||||
| 99 | |||||||||||
| 100 | 9 | $self->add_service($name, $service); | |||||||||
| 101 | } | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | 4 | ▶ | sub initialize_services { | ||||||||
|
Subroutine
initialize_services: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 106 | 4 | my $self = shift; | |||||||||
| 107 | |||||||||||
| 108 | 4 | for my $name ($self->service_names) { | |||||||||
| 109 | 9 | ▶ | next if $self->initialized_services->{ $name }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 9 | $self->initialized_services->{ $name }++; | |||||||||
| 112 | |||||||||||
| 113 | 9 | my $service = $self->get_service($name); | |||||||||
| 114 | 9 | $service->initialize; | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | |||||||||||
| 119 | 4 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 120 | 4 | my $self = shift; | |||||||||
| 121 | |||||||||||
| 122 | 4 | $self->construct_services; | |||||||||
| 123 | 4 | $self->initialize_services; | |||||||||
| 124 | |||||||||||
| 125 | 4 | $self->event_loop->run; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | 1 | ▶ | sub shutdown { | ||||||||
|
Subroutine
shutdown: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 130 | 1 | my $self = shift; | |||||||||
| 131 | |||||||||||
| 132 | 1 | $_->shutdown for ($self->list_services); | |||||||||
| 133 | 1 | $self->destroy_services; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||
| 138 | |||||||||||
| 139 | __END__ | ||||||||||