| 1 | =head1 NAME | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | Synapse::CLI::Config::Object - base class for your configuration objects | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head2 Step 1. Write one or more config objects: | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | package My::Config::User; | ||||||||||||||||||||
| 12 | use base qw /Synapse::CLI::Config::User/; | ||||||||||||||||||||
| 13 | use Synapse::CLI::Config; | ||||||||||||||||||||
| 14 | use strict; | ||||||||||||||||||||
| 15 | use warnings; | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | # optional code goes here | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1; | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | __END__ | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head2 Step 2. Write your application CLI accessor: | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | #!/usr/bin/perl | ||||||||||||||||||||
| 27 | # this is myapp-cli. It should be installed in /usr/local/bin/myapp-cli | ||||||||||||||||||||
| 28 | use Synapse::CLI::Config; | ||||||||||||||||||||
| 29 | use YAML::XS; | ||||||||||||||||||||
| 30 | use warning; | ||||||||||||||||||||
| 31 | use strict; | ||||||||||||||||||||
| 32 | $Synapse::CLI::Config::BASE_DIR = "/etc/myapp"; | ||||||||||||||||||||
| 33 | $Synapse::CLI::Config::ALIAS->{type} = 'Synapse::CLI::Config::Type'; | ||||||||||||||||||||
| 34 | $Synapse::CLI::Config::ALIAS->{user} = 'MyAPP::User'; | ||||||||||||||||||||
| 35 | print Dump (Synapse::CLI::Config::execute (@ARGV)); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head2 Step 3. Have fun on the CLI | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | myapp-cli type user create c-hiver "Clementine Hiver" | ||||||||||||||||||||
| 40 | myapp-cli user chiver set password moncontenkite | ||||||||||||||||||||
| 41 | myapp-cli user chiver set-add permission watchtv | ||||||||||||||||||||
| 42 | myapp-cli user chiver set-add permission playoncomputer | ||||||||||||||||||||
| 43 | myapp-cli user chiver set-add permission readbooks | ||||||||||||||||||||
| 44 | myapp-cli user chiver show | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||
| 47 | package Synapse::CLI::Config::Object; | ||||||||||||||||||||
| 48 | 3 | ▶ | use Synapse::CLI::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | 3 | ▶ | use Time::HiRes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 50 | 3 | ▶ | use File::Touch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 51 | 3 | ▶ | use File::Copy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 3 | ▶ | use YAML::XS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | 3 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =head1 Class methods | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | Usage: cli type object-class object-method arg1 ... argN | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head2 $class->create ($name, $label); | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | Usage: cli type mytype create foo "This is a beautiful Foo" | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | Creates an object of type $class of name $name. $name will be the object file | ||||||||||||||||||||
| 67 | name on disk. Allowed names are /^[a-z0-9-_]+$/. If no 'name' is supplied, | ||||||||||||||||||||
| 68 | $class->create() provides one. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | Optionally, sets label to "This is a beautiful Foo", which is shorter than | ||||||||||||||||||||
| 71 | doing: | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | cli type mytype create foo | ||||||||||||||||||||
| 74 | cli mytype foo set label "This is a beautiful Foo" | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||
| 77 | 1 | ▶ | sub create { | ||||||||||||||||||
|
Subroutine
create: called CC=6 SCAR=20.5
Pod
documented
| |||||||||||||||||||||
| 78 | 1 | my $class = shift; | |||||||||||||||||||
| 79 | 1 | my $objid = shift; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 1 | ▶ | defined $objid or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | my $time = Time::HiRes::time(); | |||||||||||||||||||
| 83 | 0 | $time =~ s/\./-/g; | |||||||||||||||||||
| 84 | 0 | $objid = $time; | |||||||||||||||||||
| 85 | }; | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 1 | ▶ | $objid =~ /^[a-z0-9-_]+$/i or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | Synapse::CLI::Config::debug ("invalid object name $objid"); | |||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||
| 90 | }; | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1 | my $config_dir = $class->__confdir__(); | |||||||||||||||||||
| 93 | 1 | ▶ | -d $config_dir or mkdir $config_dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 1 | ▶ | -d $config_dir or die "$config_dir does not exist and cannot be created"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 1 | File::Touch::touch ( do { my $o = bless { name => $objid }, $class; $o->__filepath__() } ); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | my $self = $class->new ($objid); | |||||||||||||||||||
| 98 | 1 | ▶ | @_ and do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 1 | $self->set (label => join ' ', @_); | |||||||||||||||||||
| 100 | 1 | return $self->__save__ ('set', 'label', @_); | |||||||||||||||||||
| 101 | }; | ||||||||||||||||||||
| 102 | 0 | return $self; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head2 $class->new($name); | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | This is one of the few methods which is NOT to be used on the CLI (although you | ||||||||||||||||||||
| 109 | could since "cli myclass myid show" is logically equivalent to the less | ||||||||||||||||||||
| 110 | intuitive "cli type myclass new myid"). | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Builds and returns object of type $class with name $name. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | 8 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 116 | 8 | my $class = shift; | |||||||||||||||||||
| 117 | 8 | my $name = shift; | |||||||||||||||||||
| 118 | 8 | my $self = bless { @_ }, $class; | |||||||||||||||||||
| 119 | 8 | $self->{name} = $name; | |||||||||||||||||||
| 120 | 8 | return $self->__init__(); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head2 $class->list(); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | Usage: cli type myclass list | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | Returns a list of object ids for this class. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||
| 131 | 3 | ▶ | sub list { | ||||||||||||||||||
|
Subroutine
list: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 132 | 3 | my $class = shift; | |||||||||||||||||||
| 133 | 3 | my $dir = $class->__confdir__(); | |||||||||||||||||||
| 134 | 3 | opendir (DIR, $dir); | |||||||||||||||||||
| 135 | 3 | my @files = readdir (DIR); | |||||||||||||||||||
| 136 | 3 | closedir (DIR); | |||||||||||||||||||
| 137 | 3 | my @res = (); | |||||||||||||||||||
| 138 | 3 | for my $f (@files) { | |||||||||||||||||||
| 139 | 8 | ▶ | $f =~ /^\.$/ and next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 5 | ▶ | $f =~ /^\..$/ and next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 2 | ▶ | -d "$dir/$f" and next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 2 | ▶ | $f =~ s/\.conf$// or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 2 | push @res, $f; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 3 | ▶ | return wantarray ? @res : \@res; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head2 $class->list_l(); | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | Usage: cli type myclass list | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Returns a list of object ids for this class, as well as the corresponding | ||||||||||||||||||||
| 154 | labels, separated with a CSV-style semicolumn. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||
| 157 | 0 | ▶ | sub list_l { | ||||||||||||||||||
|
Subroutine
list_l: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 158 | 0 | my $class = shift; | |||||||||||||||||||
| 159 | 0 | my @res = map { "$_; " . $class->new ($_)->label() } $class->list (@_); | |||||||||||||||||||
| 160 | 0 | ▶ | return wantarray ? @res : \@res; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 $class->count(); | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | usage: cli type myclass count | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | counts how many objects of myclass exist. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||
| 171 | 1 | ▶ | sub count { | ||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 172 | 1 | my $class = shift; | |||||||||||||||||||
| 173 | 1 | my @list = $class->list(); | |||||||||||||||||||
| 174 | 1 | return 0 + @list; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 Instance methods | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Usage: cli object-class object-id object-method arg1 ... argN | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =head2 $self->set($attr, $stuff); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | Usage: cli mytype myobject set foo "this is bar" | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | Sets $self->{foo} to "this is bar" | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||
| 190 | 9 | ▶ | sub set { | ||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 191 | 9 | my $self = shift; | |||||||||||||||||||
| 192 | 9 | my $attr = shift; | |||||||||||||||||||
| 193 | 9 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 194 | 9 | $self->{$attr} = $stuff; | |||||||||||||||||||
| 195 | 9 | return $self; | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =head2 $self->del($attr); | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | Usage: cli mytype myobject del foo | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | Removes $self->{foo} | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||
| 206 | 1 | ▶ | sub del { | ||||||||||||||||||
|
Subroutine
del: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 207 | 1 | my $self = shift; | |||||||||||||||||||
| 208 | 1 | my $attr = shift; | |||||||||||||||||||
| 209 | 1 | delete $self->{$attr}; | |||||||||||||||||||
| 210 | 1 | return $self; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head2 $self->list_push($attr, $stuff); | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | Usage: cli mytype myobject list-push foo "this is bar" | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | Makes $self->{foo} an empty list if not defined. Adds "this is bar" to the list. | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||
| 221 | 3 | ▶ | sub list_push { | ||||||||||||||||||
|
Subroutine
list_push: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 222 | 3 | my $self = shift; | |||||||||||||||||||
| 223 | 3 | my $attr = shift; | |||||||||||||||||||
| 224 | 3 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 225 | 3 | ▶ | $self->{$attr} ||= []; | ||||||||||||||||||
|
Condition100%2/2
$self->{$attr} ||= []
MC/DC100%1/1
$self->{$attr} ||= []
$self->{$attr}
Truth table100%2/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 226 | 3 | push @{$self->{$attr}}, $stuff; | |||||||||||||||||||
| 227 | 3 | return $self; | |||||||||||||||||||
| 228 | } | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =head2 $self->list_pop($attr) | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | Usage: cli mytype myobject list-pop foo | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | Makes $self->{foo} an empty list if not defined. Then pops the list. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||
| 238 | 1 | ▶ | sub list_pop { | ||||||||||||||||||
|
Subroutine
list_pop: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 239 | 1 | my $self = shift; | |||||||||||||||||||
| 240 | 1 | my $attr = shift; | |||||||||||||||||||
| 241 | 1 | ▶ | $self->{$attr} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attr} ||= []
MC/DC0%0/1
$self->{$attr} ||= []
$self->{$attr}
Truth table50%1/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 242 | 1 | pop @{$self->{$attr}}; | |||||||||||||||||||
| 243 | 1 | return $self; | |||||||||||||||||||
| 244 | } | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =head2 $self->list_shift($attr) | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | Usage: cli mytype myobject list-shift foo | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | Makes $self->{foo} an empty list if not defined. Then shifts the list. | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||
| 254 | 1 | ▶ | sub list_shift { | ||||||||||||||||||
|
Subroutine
list_shift: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 255 | 1 | my $self = shift; | |||||||||||||||||||
| 256 | 1 | my $attr = shift; | |||||||||||||||||||
| 257 | 1 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 258 | 1 | ▶ | $self->{$attr} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attr} ||= []
MC/DC0%0/1
$self->{$attr} ||= []
$self->{$attr}
Truth table50%1/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 259 | 1 | shift @{$self->{$attr}}; | |||||||||||||||||||
| 260 | 1 | return $self; | |||||||||||||||||||
| 261 | } | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | =head2 $self->list_unshift($attr, $stuff) | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | Usage: cli mytype myobject list-unshift foo "this is bar" | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | Makes $self->{foo} an empty list if not defined. Then unshifts the list with | ||||||||||||||||||||
| 269 | "this is bar". | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | =cut | ||||||||||||||||||||
| 272 | 1 | ▶ | sub list_unshift { | ||||||||||||||||||
|
Subroutine
list_unshift: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 273 | 1 | my $self = shift; | |||||||||||||||||||
| 274 | 1 | my $attr = shift; | |||||||||||||||||||
| 275 | 1 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 276 | 1 | ▶ | $self->{$attr} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attr} ||= []
MC/DC0%0/1
$self->{$attr} ||= []
$self->{$attr}
Truth table50%1/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 277 | 1 | unshift @{$self->{$attr}}, $stuff; | |||||||||||||||||||
| 278 | 1 | return $self; | |||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | =head2 $self->list_del($attr, $index) | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | Usage: cli mytype myobject list-del foo | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | Removes $self->{$attr}->{$index} from the list. | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||
| 289 | 1 | ▶ | sub list_del { | ||||||||||||||||||
|
Subroutine
list_del: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 290 | 1 | my $self = shift; | |||||||||||||||||||
| 291 | 1 | my $attr = shift; | |||||||||||||||||||
| 292 | 1 | my $index = shift; | |||||||||||||||||||
| 293 | 1 | ▶ | $self->{$attr} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attr} ||= []
MC/DC0%0/1
$self->{$attr} ||= []
$self->{$attr}
Truth table50%1/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 294 | 1 | splice @{$self->{$attr}}, $index, 1; | |||||||||||||||||||
| 295 | } | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | =head2 $self->list_add($attr, $index, $stuff) | ||||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | Usage: cli mytype myobject list-add foo "this is stuff" | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | Adds "this is stuff" at position $index in $self->{$attr} list. | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||
| 305 | 1 | ▶ | sub list_add { | ||||||||||||||||||
|
Subroutine
list_add: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 306 | 1 | my $self = shift; | |||||||||||||||||||
| 307 | 1 | my $attr = shift; | |||||||||||||||||||
| 308 | 1 | my $index = shift; | |||||||||||||||||||
| 309 | 1 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 310 | 1 | ▶ | $self->{$attr} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attr} ||= []
MC/DC0%0/1
$self->{$attr} ||= []
$self->{$attr}
Truth table50%1/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 311 | 1 | splice @{$self->{$attr}}, $index, 0, $stuff; | |||||||||||||||||||
| 312 | } | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | =head2 $self->set_add($attr, $stuff) | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | Usage: cli mytype myobject set-add foo "this is stuff" | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | Treats $self->{$attr} as a set (creating it if needed, using a hash ref), and | ||||||||||||||||||||
| 320 | adds "this is stuff" to the set. | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | =cut | ||||||||||||||||||||
| 323 | 5 | ▶ | sub set_add { | ||||||||||||||||||
|
Subroutine
set_add: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 324 | 5 | my $self = shift; | |||||||||||||||||||
| 325 | 5 | my $attr = shift; | |||||||||||||||||||
| 326 | 5 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 327 | 5 | ▶ | $self->{$attr} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$self->{$attr} ||= {}
MC/DC100%1/1
$self->{$attr} ||= {}
$self->{$attr}
Truth table100%2/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 328 | 5 | $self->{$attr}->{$stuff} = 1; | |||||||||||||||||||
| 329 | 5 | return $self; | |||||||||||||||||||
| 330 | } | ||||||||||||||||||||
| 331 | |||||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | =head2 $self->set_del($attr, $stuff) | ||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | Usage: cli mytype myobject set-del foo "this is stuff" | ||||||||||||||||||||
| 336 | |||||||||||||||||||||
| 337 | Removes "this is stuff" from the set. | ||||||||||||||||||||
| 338 | |||||||||||||||||||||
| 339 | =cut | ||||||||||||||||||||
| 340 | 0 | ▶ | sub set_del { | ||||||||||||||||||
|
Subroutine
set_del: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 341 | 0 | my $self = shift; | |||||||||||||||||||
| 342 | 0 | my $attr = shift; | |||||||||||||||||||
| 343 | 0 | my $stuff = join ' ', @_; | |||||||||||||||||||
| 344 | 0 | ▶ | $self->{$attr} ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{$attr} ||= {}
MC/DC0%0/1
$self->{$attr} ||= {}
$self->{$attr}
Truth table0%0/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 345 | 0 | delete $self->{$attr}->{$stuff}; | |||||||||||||||||||
| 346 | 0 | return $self; | |||||||||||||||||||
| 347 | } | ||||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | =head2 $self->set_list($attr) | ||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | Usage: cli mytype myobject set-list foo | ||||||||||||||||||||
| 353 | |||||||||||||||||||||
| 354 | Lists all items in the set. | ||||||||||||||||||||
| 355 | |||||||||||||||||||||
| 356 | =cut | ||||||||||||||||||||
| 357 | 1 | ▶ | sub set_list { | ||||||||||||||||||
|
Subroutine
set_list: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 358 | 1 | my $self = shift; | |||||||||||||||||||
| 359 | 1 | my $attr = shift; | |||||||||||||||||||
| 360 | 1 | ▶ | $self->{$attr} ||= {}; | ||||||||||||||||||
|
Condition50%1/2
$self->{$attr} ||= {}
MC/DC0%0/1
$self->{$attr} ||= {}
$self->{$attr}
Truth table50%1/2
A
A= $self->{$attr}
| |||||||||||||||||||||
| 361 | return wantarray ? | ||||||||||||||||||||
| 362 | 0 | sort keys %{$self->{$attr}} : | |||||||||||||||||||
| 363 | 1 | ▶ | [ sort keys %{$self->{$attr}} ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 364 | } | ||||||||||||||||||||
| 365 | |||||||||||||||||||||
| 366 | |||||||||||||||||||||
| 367 | =head2 $self->label() | ||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | Usage: cli mytype myobject label | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | Each object as an optional label associated with it, this method returns it. | ||||||||||||||||||||
| 372 | Returns name() is label is not defined. | ||||||||||||||||||||
| 373 | |||||||||||||||||||||
| 374 | =cut | ||||||||||||||||||||
| 375 | 4 | ▶ | sub label { | ||||||||||||||||||
|
Subroutine
label: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 376 | 4 | my $self = shift; | |||||||||||||||||||
| 377 | 4 | ▶ | return $self->{label} || $self->name(); | ||||||||||||||||||
|
Condition33%1/3
$self->{'label'} || $self->name
MC/DC0%0/2
$self->{'label'} || $self->name
$self->{'label'}
$self->name
Truth table33%1/3
A || B
A= $self->{'label'}B= $self->name
| |||||||||||||||||||||
| 378 | } | ||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | |||||||||||||||||||||
| 381 | =head2 $self->name() | ||||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | Usage: cli mytype myobject name | ||||||||||||||||||||
| 384 | |||||||||||||||||||||
| 385 | Each object as a name. Returns it. | ||||||||||||||||||||
| 386 | |||||||||||||||||||||
| 387 | =cut | ||||||||||||||||||||
| 388 | 20 | ▶ | sub name { | ||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 389 | 20 | my $self = shift; | |||||||||||||||||||
| 390 | 20 | ▶ | return $self->{name} || $self->name(); | ||||||||||||||||||
|
Condition33%1/3
$self->{'name'} || $self->name
MC/DC0%0/2
$self->{'name'} || $self->name
$self->{'name'}
$self->name
Truth table33%1/3
A || B
A= $self->{'name'}B= $self->name
| |||||||||||||||||||||
| 391 | } | ||||||||||||||||||||
| 392 | |||||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | =head2 $self->show(); | ||||||||||||||||||||
| 395 | |||||||||||||||||||||
| 396 | Usage: cli mytype myobject show | ||||||||||||||||||||
| 397 | |||||||||||||||||||||
| 398 | From Perl, this method is not very interesting since it just returns $self. But | ||||||||||||||||||||
| 399 | on the CLI, will display a YAML representation of $self, which is handy to view | ||||||||||||||||||||
| 400 | your objects. | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | =cut | ||||||||||||||||||||
| 403 | 0 | ▶ | sub show { return shift } | ||||||||||||||||||
|
Subroutine
show: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 404 | |||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | =head2 $self->rename_to ($newname); | ||||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | Usage: cli mytype foo rename-to bar | ||||||||||||||||||||
| 409 | |||||||||||||||||||||
| 410 | Changes the object 'name' attribute as well as file name on disk. | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | =cut | ||||||||||||||||||||
| 413 | 0 | ▶ | sub rename_to_FORCE_NOSAVE { 1 } | ||||||||||||||||||
|
Subroutine
rename_to_FORCE_NOSAVE: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 414 | 1 | ▶ | sub rename_to { | ||||||||||||||||||
|
Subroutine
rename_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 415 | 1 | my $self = shift; | |||||||||||||||||||
| 416 | 1 | my $newid = shift; | |||||||||||||||||||
| 417 | 1 | my $path1 = $self->__filepath__(); | |||||||||||||||||||
| 418 | 1 | my $path2 = $path1; | |||||||||||||||||||
| 419 | 1 | my @path2 = split /\//, $path2; | |||||||||||||||||||
| 420 | 1 | pop (@path2); | |||||||||||||||||||
| 421 | 1 | push @path2, "$newid.conf"; | |||||||||||||||||||
| 422 | 1 | $path2 = join '/', @path2; | |||||||||||||||||||
| 423 | 1 | File::Copy::move ($path1, $path2); | |||||||||||||||||||
| 424 | 1 | $self->{name} = $newid; | |||||||||||||||||||
| 425 | 1 | return $self; | |||||||||||||||||||
| 426 | } | ||||||||||||||||||||
| 427 | |||||||||||||||||||||
| 428 | |||||||||||||||||||||
| 429 | =head2 $self->copy_as ($newname); | ||||||||||||||||||||
| 430 | |||||||||||||||||||||
| 431 | Usage: cli mytype foo copy-as bar | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | Copies the object and names the copy $newname. Returns the newly copied object. | ||||||||||||||||||||
| 434 | |||||||||||||||||||||
| 435 | =cut | ||||||||||||||||||||
| 436 | 1 | ▶ | sub copy_as { | ||||||||||||||||||
|
Subroutine
copy_as: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 437 | 1 | my $self = shift; | |||||||||||||||||||
| 438 | 1 | my $newid = shift; | |||||||||||||||||||
| 439 | 1 | my $path1 = $self->__filepath__(); | |||||||||||||||||||
| 440 | 1 | my $path2 = $path1; | |||||||||||||||||||
| 441 | 1 | my @path2 = split /\//, $path2; | |||||||||||||||||||
| 442 | 1 | pop (@path2); | |||||||||||||||||||
| 443 | 1 | push @path2, "$newid.conf"; | |||||||||||||||||||
| 444 | 1 | $path2 = join '/', @path2; | |||||||||||||||||||
| 445 | 1 | File::Copy::copy ($path1, $path2); | |||||||||||||||||||
| 446 | 1 | my $class = ref $self; | |||||||||||||||||||
| 447 | 1 | return $class->new ($newid); | |||||||||||||||||||
| 448 | } | ||||||||||||||||||||
| 449 | |||||||||||||||||||||
| 450 | |||||||||||||||||||||
| 451 | =head2 $self->remove(); | ||||||||||||||||||||
| 452 | |||||||||||||||||||||
| 453 | Usage: cli mytype foo remove | ||||||||||||||||||||
| 454 | |||||||||||||||||||||
| 455 | Removes foo. | ||||||||||||||||||||
| 456 | |||||||||||||||||||||
| 457 | =cut | ||||||||||||||||||||
| 458 | 2 | ▶ | sub remove { | ||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 459 | 2 | my $self = shift; | |||||||||||||||||||
| 460 | 2 | my $new = shift; | |||||||||||||||||||
| 461 | 2 | unlink $self->__filepath__(); | |||||||||||||||||||
| 462 | } | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | |||||||||||||||||||||
| 465 | ## PRIVATE METHODS : if you made it this far and want to override them anyways, | ||||||||||||||||||||
| 466 | ## then you probably should | ||||||||||||||||||||
| 467 | |||||||||||||||||||||
| 468 | |||||||||||||||||||||
| 469 | 8 | ▶ | sub __init__ { | ||||||||||||||||||
|
Subroutine
__init__: called CC=5 SCAR=16.9
| |||||||||||||||||||||
| 470 | 8 | my $self = shift; | |||||||||||||||||||
| 471 | 8 | my $path = $self->__filepath__(); | |||||||||||||||||||
| 472 | 8 | ▶ | -e $path or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 473 | |||||||||||||||||||||
| 474 | 8 | my $fh = IO::File->new(); | |||||||||||||||||||
| 475 | 8 | ▶ | if ($fh->open("<$path")) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 476 | 8 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 477 | 7 | chomp($line); | |||||||||||||||||||
| 478 | 7 | my ($timestamp, $method, @arguments) = split /\s+/, $line; | |||||||||||||||||||
| 479 | 7 | ▶ | $self->can ($method) or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 480 | 0 | Synapse::CLI::Config::debug ("cannot invoke method $method on object $self - ignoring"); | |||||||||||||||||||
| 481 | }; | ||||||||||||||||||||
| 482 | 7 | $self->$method (@arguments); | |||||||||||||||||||
| 483 | } | ||||||||||||||||||||
| 484 | 8 | undef $fh; | |||||||||||||||||||
| 485 | } | ||||||||||||||||||||
| 486 | else { | ||||||||||||||||||||
| 487 | 0 | Synapse::CLI::Config::debug ("cannot instantiate : file is not readable"); | |||||||||||||||||||
| 488 | 0 | return; | |||||||||||||||||||
| 489 | }; | ||||||||||||||||||||
| 490 | 8 | return $self; | |||||||||||||||||||
| 491 | } | ||||||||||||||||||||
| 492 | |||||||||||||||||||||
| 493 | |||||||||||||||||||||
| 494 | 18 | ▶ | sub __confdir__ { | ||||||||||||||||||
|
Subroutine
__confdir__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 495 | 18 | my $self = shift; | |||||||||||||||||||
| 496 | 18 | ▶ | my $class = 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
| |||||||||||||||||||||
| 497 | 18 | $class =~ s/::/-/g; | |||||||||||||||||||
| 498 | 18 | return Synapse::CLI::Config::base_dir() . "/$class"; | |||||||||||||||||||
| 499 | } | ||||||||||||||||||||
| 500 | |||||||||||||||||||||
| 501 | |||||||||||||||||||||
| 502 | 14 | ▶ | sub __filepath__ { | ||||||||||||||||||
|
Subroutine
__filepath__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 503 | 14 | my $self = shift; | |||||||||||||||||||
| 504 | 14 | return $self->__confdir__() . '/' . $self->name() . '.conf'; | |||||||||||||||||||
| 505 | } | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | |||||||||||||||||||||
| 508 | 1 | ▶ | sub __save__ { | ||||||||||||||||||
|
Subroutine
__save__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 509 | 1 | my $self = shift; | |||||||||||||||||||
| 510 | 1 | my $objid = $self->name(); | |||||||||||||||||||
| 511 | 1 | my $method = shift; | |||||||||||||||||||
| 512 | 1 | my $args = join ' ', @_; | |||||||||||||||||||
| 513 | 1 | my $time = Time::HiRes::time(); | |||||||||||||||||||
| 514 | 1 | my $path = $self->__filepath__(); | |||||||||||||||||||
| 515 | 1 | ▶ | open OBJECT, ">>$path" or die "cannot write-open $path"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 516 | 1 | print OBJECT "$time $method $args\n"; | |||||||||||||||||||
| 517 | 1 | close OBJECT; | |||||||||||||||||||
| 518 | 1 | return $self; | |||||||||||||||||||
| 519 | } | ||||||||||||||||||||
| 520 | |||||||||||||||||||||
| 521 | |||||||||||||||||||||
| 522 | 3 | 1; | |||||||||||||||||||
| 523 | |||||||||||||||||||||
| 524 | |||||||||||||||||||||
| 525 | __END__ | ||||||||||||||||||||