line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Balancer::Model; |
2
|
1
|
|
|
1
|
|
8
|
use Modern::Perl; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
20
|
|
3
|
1
|
|
|
1
|
|
1240
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use File::Spec; |
5
|
|
|
|
|
|
|
use Path::Tiny qw(!path); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
with qw(HTTP::Balancer::Role); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
HTTP::Balancer::Model - the base class of models of HTTP::Balancer |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
package HTTP::Balancer::Model::Foo; |
16
|
|
|
|
|
|
|
use Modern::Perl; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use Moose; |
19
|
|
|
|
|
|
|
extends qw(HTTP::Balancer::Model); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use MoooseX::Storage; |
22
|
|
|
|
|
|
|
with Storage(format => 'YAML', io => 'File'); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 FUNCTIONS AND METHODS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 models |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
returns the list of last name of HTTP::Balancer::Model::* |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub models { |
33
|
|
|
|
|
|
|
my $class = ref($_[0]) ? ref(shift) : shift; |
34
|
|
|
|
|
|
|
require Namespace::Dispatch; |
35
|
|
|
|
|
|
|
map { $class->model($_) } |
36
|
|
|
|
|
|
|
@{Namespace::Dispatch::leaves($class)}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 model_name |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
class method and instance method |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
returns lowercase of last name of current model |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub model_name { |
48
|
|
|
|
|
|
|
my ($self, ) = @_; |
49
|
|
|
|
|
|
|
my $ref = ref($self) || $self; |
50
|
|
|
|
|
|
|
$ref =~ s{HTTP::Balancer::Model::}{}; |
51
|
|
|
|
|
|
|
return lc($ref); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 model_dir |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
class method and instance method |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
returns the directory store the entities of current model |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub model_dir { |
63
|
|
|
|
|
|
|
my ($self, ) = @_; |
64
|
|
|
|
|
|
|
File::Spec->catdir( |
65
|
|
|
|
|
|
|
$self->config->dbpath, |
66
|
|
|
|
|
|
|
$self->model_name |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 path |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
instance method |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
returns the path for storing current object |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub path { |
79
|
|
|
|
|
|
|
my ($self, ) = @_; |
80
|
|
|
|
|
|
|
$self->id ? |
81
|
|
|
|
|
|
|
File::Spec->catfile( |
82
|
|
|
|
|
|
|
$self->model_dir, |
83
|
|
|
|
|
|
|
$self->id |
84
|
|
|
|
|
|
|
) |
85
|
|
|
|
|
|
|
: undef; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 glob |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
class method |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
returns all entities stored in model_dir, sorted with id. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub glob { |
97
|
|
|
|
|
|
|
my ($self, ) = @_; |
98
|
|
|
|
|
|
|
sort { |
99
|
|
|
|
|
|
|
[File::Spec->splitpath($a)]->[-1] <=> [File::Spec->splitpath($b)]->[-1] |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
glob(File::Spec->catfile($self->model_dir, "*")); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 save |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
instance method |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
save current object into model_dir, named as its id. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
generate auto-incremented id for new object not on disk yet. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub save { |
115
|
|
|
|
|
|
|
my ($self, ) = @_; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
unless ($self->id) { |
118
|
|
|
|
|
|
|
my $last = [$self->glob]->[-1]; |
119
|
|
|
|
|
|
|
my $num = $last ? [File::Spec->splitpath($last)]->[-1] : 0; |
120
|
|
|
|
|
|
|
$self->id($num+1) |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$self->store($self->path); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 all(\&closure) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
class method |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
returns all object restored from disk |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
call \&closure on each instance if given. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub all { |
137
|
|
|
|
|
|
|
my ($self, $closure) = @_; |
138
|
|
|
|
|
|
|
map { $closure ? $closure->($_) : $_ } |
139
|
|
|
|
|
|
|
map { $self->load($_) } $self->glob; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 find($attr => $value) |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
class method |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
returns the first object satisfying the condition from disk. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub find { |
151
|
|
|
|
|
|
|
my ($self, $attr, $value) = @_; |
152
|
|
|
|
|
|
|
for ($self->all) { |
153
|
|
|
|
|
|
|
return $_ if $_->$attr eq $value; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 where($attr => $value) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
class method |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
returns all objects satisfying the condition from disk |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=cut |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub where { |
166
|
|
|
|
|
|
|
my ($self, $attr, $value) = @_; |
167
|
|
|
|
|
|
|
grep { |
168
|
|
|
|
|
|
|
$_->$attr eq $value |
169
|
|
|
|
|
|
|
} $self->all; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 remove |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
remove the instance from disk. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub remove { |
179
|
|
|
|
|
|
|
my ($self, ) = @_; |
180
|
|
|
|
|
|
|
Path::Tiny::path($self->path)->remove or die $@; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 columns |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
list names of all columns of current Model. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub columns { |
190
|
|
|
|
|
|
|
my ($self, ) = @_; |
191
|
|
|
|
|
|
|
map { $_->name } $self->meta->get_all_attributes; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 slice(@columns) |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
returns attributes slice |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub slice { |
202
|
|
|
|
|
|
|
my ($self, @columns) = @_; |
203
|
|
|
|
|
|
|
map { $self->$_ } @columns; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
__END__ |
208
|
|
|
|
|
|
|
|