| 1 | package HTTP::Balancer::Model; | ||||||||||||||||||||
| 2 | 2 | ▶ | use Modern::Perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Path::Tiny qw(!path); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | 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 | 1 | ▶ | sub models { | ||||||||||||||||||
|
Subroutine
models: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 33 | 1 | ▶ | my $class = ref($_[0]) ? ref(shift) : shift; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 1 | require Namespace::Dispatch; | |||||||||||||||||||
| 35 | 2 | map { $class->model($_) } | |||||||||||||||||||
| 36 | 1 | @{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 | 71 | ▶ | sub model_name { | ||||||||||||||||||
|
Subroutine
model_name: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 48 | 71 | my ($self, ) = @_; | |||||||||||||||||||
| 49 | 71 | ▶ | my $ref = ref($self) || $self; | ||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 50 | 71 | $ref =~ s{HTTP::Balancer::Model::}{}; | |||||||||||||||||||
| 51 | 71 | 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 | 70 | ▶ | sub model_dir { | ||||||||||||||||||
|
Subroutine
model_dir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 63 | 70 | my ($self, ) = @_; | |||||||||||||||||||
| 64 | 70 | 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 | 33 | ▶ | sub path { | ||||||||||||||||||
|
Subroutine
path: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 79 | 33 | my ($self, ) = @_; | |||||||||||||||||||
| 80 | 33 | ▶ | $self->id ? | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 35 | ▶ | sub glob { | ||||||||||||||||||
|
Subroutine
glob: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 97 | 35 | my ($self, ) = @_; | |||||||||||||||||||
| 98 | sort { | ||||||||||||||||||||
| 99 | 35 | [File::Spec->splitpath($a)]->[-1] <=> [File::Spec->splitpath($b)]->[-1] | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 2 | ▶ | glob(File::Spec->catfile($self->model_dir, "*")); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 31 | ▶ | sub save { | ||||||||||||||||||
|
Subroutine
save: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 115 | 31 | my ($self, ) = @_; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 31 | ▶ | unless ($self->id) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 31 | my $last = [$self->glob]->[-1]; | |||||||||||||||||||
| 119 | 31 | ▶ | my $num = $last ? [File::Spec->splitpath($last)]->[-1] : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 31 | $self->id($num+1) | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 31 | $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 | 3 | ▶ | sub all { | ||||||||||||||||||
|
Subroutine
all: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 137 | 3 | my ($self, $closure) = @_; | |||||||||||||||||||
| 138 | 93 | ▶ | map { $closure ? $closure->($_) : $_ } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 3 | 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 | 1 | ▶ | sub find { | ||||||||||||||||||
|
Subroutine
find: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 151 | 1 | my ($self, $attr, $value) = @_; | |||||||||||||||||||
| 152 | 1 | for ($self->all) { | |||||||||||||||||||
| 153 | 5 | ▶ | return $_ if $_->$attr eq $value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 1 | ▶ | sub where { | ||||||||||||||||||
|
Subroutine
where: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 166 | 1 | my ($self, $attr, $value) = @_; | |||||||||||||||||||
| 167 | grep { | ||||||||||||||||||||
| 168 | 1 | $_->$attr eq $value | |||||||||||||||||||
| 169 | } $self->all; | ||||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head2 remove | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | remove the instance from disk. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 0 | ▶ | sub remove { | ||||||||||||||||||
|
Subroutine
remove: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 179 | 0 | my ($self, ) = @_; | |||||||||||||||||||
| 180 | 0 | ▶ | Path::Tiny::path($self->path)->remove or die $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =head2 columns | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | list names of all columns of current Model. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 0 | ▶ | sub columns { | ||||||||||||||||||
|
Subroutine
columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 190 | 0 | my ($self, ) = @_; | |||||||||||||||||||
| 191 | 0 | map { $_->name } $self->meta->get_all_attributes; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =head2 slice(@columns) | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | returns attributes slice | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 0 | ▶ | sub slice { | ||||||||||||||||||
|
Subroutine
slice: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 202 | 0 | my ($self, @columns) = @_; | |||||||||||||||||||
| 203 | 0 | map { $self->$_ } @columns; | |||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 2 | 1; | |||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||