line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::Brain; |
2
|
|
|
|
|
|
|
$Hubot::Brain::VERSION = '0.2.7'; |
3
|
1
|
|
|
1
|
|
1396
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use namespace::autoclean; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
extends 'Hubot::EventEmitter'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub BUILD { |
9
|
|
|
|
|
|
|
my $self = shift; |
10
|
|
|
|
|
|
|
$self->{data}{users} = {}; |
11
|
|
|
|
|
|
|
} |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub save { |
14
|
|
|
|
|
|
|
my $self = shift; |
15
|
|
|
|
|
|
|
$self->emit( 'save', $self->{data} ); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub close { |
19
|
|
|
|
|
|
|
my $self = shift; |
20
|
|
|
|
|
|
|
$self->save; |
21
|
|
|
|
|
|
|
$self->emit('close'); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub mergeData { |
25
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
26
|
|
|
|
|
|
|
for my $key ( keys %$data ) { |
27
|
|
|
|
|
|
|
if ( $key eq 'users' ) { |
28
|
|
|
|
|
|
|
for my $k ( keys %{ $data->{$key} } ) { |
29
|
|
|
|
|
|
|
bless $data->{$key}{$k}, 'Hubot::User'; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$self->{data}{$key} = $data->{$key}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$self->emit( 'loaded', $self->{data} ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
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.7 |
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 |