| 1 | package Hubot::Brain; | ||||||||||
| 2 | 6 | $Hubot::Brain::VERSION = '0.2.8'; | |||||||||
| 3 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | extends 'Hubot::EventEmitter'; | |||||||||
| 7 | |||||||||||
| 8 | 5 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 9 | 5 | my $self = shift; | |||||||||
| 10 | 5 | $self->{data}{users} = {}; | |||||||||
| 11 | } | ||||||||||
| 12 | |||||||||||
| 13 | 0 | ▶ | sub save { | ||||||||
|
Subroutine
save: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 14 | 0 | my $self = shift; | |||||||||
| 15 | 0 | $self->emit( 'save', $self->{data} ); | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub close { | ||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 19 | 0 | my $self = shift; | |||||||||
| 20 | 0 | $self->save; | |||||||||
| 21 | 0 | $self->emit('close'); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | sub mergeData { | ||||||||
|
Subroutine
mergeData: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 25 | 0 | my ( $self, $data ) = @_; | |||||||||
| 26 | 0 | for my $key ( keys %$data ) { | |||||||||
| 27 | 0 | ▶ | if ( $key eq 'users' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | for my $k ( keys %{ $data->{$key} } ) { | |||||||||
| 29 | 0 | bless $data->{$key}{$k}, 'Hubot::User'; | |||||||||
| 30 | } | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 0 | $self->{data}{$key} = $data->{$key}; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | $self->emit( 'loaded', $self->{data} ); | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||
| 40 | |||||||||||
| 41 | 6 | 1; | |||||||||
| 42 | |||||||||||
| 43 | =pod | ||||||||||
| 44 | |||||||||||
| 45 | =encoding utf-8 | ||||||||||
| 46 | |||||||||||
| 47 | =head1 NAME | ||||||||||
| 48 | |||||||||||
| 49 | Hubot::Brain - Represents somewhat persistent storage for the robot. | ||||||||||
| 50 | |||||||||||
| 51 | =head1 VERSION | ||||||||||
| 52 | |||||||||||
| 53 | version 0.2.8 | ||||||||||
| 54 | |||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||
| 56 | |||||||||||
| 57 | $robot->brain->{data}{key} = ''; # scalar | ||||||||||
| 58 | $robot->brain->{data}{key} = {}; # HashRef | ||||||||||
| 59 | $robot->brain->{data}{key} = []; # ArrayRef | ||||||||||
| 60 | |||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||
| 62 | |||||||||||
| 63 | Brain with external storage like a L<Hubot::Scrips::redisBrain>, C<value> must be a B<Scalar> or B<HashRef> or B<ArrayRef>. | ||||||||||
| 64 | |||||||||||
| 65 | C<$robot-E<gt>brain-E<gt>data> will convert to json string and stored to external storage. | ||||||||||
| 66 | so, if you trying to store perl object, it will fail. | ||||||||||
| 67 | |||||||||||
| 68 | without external storage, everything is fine to store to memory. | ||||||||||
| 69 | |||||||||||
| 70 | =head1 USE EXTERNAL STORAGE | ||||||||||
| 71 | |||||||||||
| 72 | =over | ||||||||||
| 73 | |||||||||||
| 74 | =item step 1 | ||||||||||
| 75 | |||||||||||
| 76 | subscribe brain's C<save> and C<close> event. | ||||||||||
| 77 | |||||||||||
| 78 | =item step 2 | ||||||||||
| 79 | |||||||||||
| 80 | robot boots time, C<save> will emitted. | ||||||||||
| 81 | robot shutdown time, C<close> will emitted. | ||||||||||
| 82 | |||||||||||
| 83 | =back | ||||||||||
| 84 | |||||||||||
| 85 | my $externalStorage = Great::Big::White::World->new; | ||||||||||
| 86 | $robot->brain->on( | ||||||||||
| 87 | 'save', | ||||||||||
| 88 | sub { | ||||||||||
| 89 | my ($e, $data) = @_; | ||||||||||
| 90 | $externalStorage->save($data); | ||||||||||
| 91 | } | ||||||||||
| 92 | ); | ||||||||||
| 93 | |||||||||||
| 94 | $robot->brain->on( | ||||||||||
| 95 | 'close', | ||||||||||
| 96 | sub { | ||||||||||
| 97 | $externalStorage->quit; | ||||||||||
| 98 | } | ||||||||||
| 99 | ); | ||||||||||
| 100 | |||||||||||
| 101 | =head1 SEE ALSO | ||||||||||
| 102 | |||||||||||
| 103 | L<Hubot::Scripts::redisBrain> | ||||||||||
| 104 | |||||||||||
| 105 | =head1 AUTHOR | ||||||||||
| 106 | |||||||||||
| 107 | Hyungsuk Hong <hshong@perl.kr> | ||||||||||
| 108 | |||||||||||
| 109 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 110 | |||||||||||
| 111 | This software is copyright (c) 2012 by Hyungsuk Hong. | ||||||||||
| 112 | |||||||||||
| 113 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 114 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||