| 1 | package Exobrain::Agent; | ||
| 2 | 2 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Exobrain; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use Exobrain::Types qw(Exobrain); | ||
| 5 | |||
| 6 | # ABSTRACT: Agent role for Exobrain agents | ||
| 7 | our $VERSION = '1.08'; # VERSION | ||
| 8 | |||
| 9 | |||
| 10 | has exobrain => ( | ||
| 11 | is => 'ro', | ||
| 12 | isa => Exobrain, | ||
| 13 | lazy => 1, | ||
| 14 | builder => '_build_exobrain', | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub _build_exobrain { return Exobrain->new; } | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | has component => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'Str', | ||
| 24 | lazy => 1, | ||
| 25 | builder => '_build_component', | ||
| 26 | ); | ||
| 27 | |||
| 28 | method _build_component() { | ||
| 29 | if (my $method = $self->can("component_name") ) { | ||
| 30 | return $self->$method; | ||
| 31 | } | ||
| 32 | |||
| 33 | my $component = ref($self); | ||
| 34 | $component =~ s/Exobrain::Agent:://; | ||
| 35 | return $component; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | has config => ( | ||
| 40 | is => 'ro', | ||
| 41 | lazy => 1, | ||
| 42 | builder => '_build_config', | ||
| 43 | ); | ||
| 44 | |||
| 45 | # By using the component name as the heading for our config, it's easy | ||
| 46 | # to clearly tell which config belongs where. | ||
| 47 | |||
| 48 | method _build_config() { | ||
| 49 | return $self->exobrain->config->{ $self->component }; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | has cache => ( | ||
| 54 | is => 'ro', | ||
| 55 | lazy => 1, | ||
| 56 | builder => '_build_cache', | ||
| 57 | ); | ||
| 58 | |||
| 59 | method _build_cache() { | ||
| 60 | eval "use Exobrain::Cache; 1;" or die $@; # Lazy load module | ||
| 61 | return Exobrain::Cache->new( namespace => $self->component ); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | has json => ( | ||
| 66 | is => 'ro', | ||
| 67 | lazy => 1, | ||
| 68 | builder => '_build_json', | ||
| 69 | ); | ||
| 70 | |||
| 71 | method _build_json() { | ||
| 72 | eval "use JSON::Any; 1;" or die $@; | ||
| 73 | return JSON::Any->new; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ | ||