Attributes/Simple.pm

filter: Statementstmt 100% 68 / 68 Branchbran 100% 4 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 97.9% 95 / 97 CC 4 SCAR 3.9
CC
4
Cov
99%
_attribute_file
3.0
_save
2.0
BEGIN
1.0
CRAP 1.5
« Attributes.pm ↑ Summary Attributes/Base.pm »
1 #!/usr/bin/perl
2 # Simple.pm
3 # Copyright (c) 2006 Jonathan Rockway <jrockway@cpan.org>
4  
5 # see POD after __END__
6  
7 package File::Attributes::Simple;
8 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use base qw(File::Attributes::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Best [ [ qw/YAML::Syck YAML/ ], qw/DumpFile LoadFile/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 our $VERSION = '0.04';
15  
16 6 ▶sub priority {
Subroutine priority: called CC=1 SCAR=0.0
Pod documented
17 6 return 1; # try something else first, eh?
18 }
19  
20 29 ▶sub applicable {
Subroutine applicable: called CC=1 SCAR=0.0
Pod documented
21 29 return 1; # this module Works Everywhere, hopefully.
22 }
23  
24 60 ▶sub _load {
Subroutine _load: called CC=1 SCAR=0.0
25 60 my $self = shift;
26 60 my $file = shift;
27 60 my $attrfile = $self->_attribute_file($file);
28
29 # throws an exception if attrfile ain't YAML (or doesn't exist, etc.)
30 60 my $data = LoadFile($attrfile);
31 48 return $data;
32 }
33  
34 26 ▶sub _save {
Subroutine _save: called CC=2 SCAR=6.9
35 26 my $self = shift;
36 26 my $file = shift;
37 26 my $data = shift;
38 26 my $attrfile = $self->_attribute_file($file);
39  
40 26 ▶ if(!scalar keys %$data){
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar keys %$data) { } 4 22 26
41 4 unlink $attrfile;
42 }
43 else {
44 22 DumpFile($attrfile, $data);
45 }
46 }
47  
48 9 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
49 9 my $self = shift;
50 9 my $file = shift;
51 9 my $data = {};
52  
53 9 eval {
54 9 $data = $self->_load($file);
55 };
56  
57 9 return keys %{$data};
58 }
59  
60 25 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
61 25 my $self = shift;
62 25 my $file = shift;
63 25 my $attr = shift;
64 25 my $data = $self->_load($file);
65
66 25 return $data->{$attr};
67 }
68  
69 16 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
70 16 my $self = shift;
71 16 my $file = shift;
72 16 my $key = shift;
73 16 my $value = shift;
74  
75 16 my $data = {};
76
77 16 eval {
78 16 $data = $self->_load($file);
79 };
80
81 16 $data->{$key} = $value;
82 16 $self->_save($file, $data);
83 16 return 1;
84 }
85  
86 10 ▶sub unset {
Subroutine unset: called CC=1 SCAR=0.0
Pod documented
87 10 my $self = shift;
88 10 my $file = shift;
89 10 my $key = shift;
90
91 10 my $data = {};
92 10 eval {
93 10 $data = $self->_load($file);
94 };
95
96 10 delete $data->{$key};
97 10 $self->_save($file, $data);
98 10 return 1;
99 }
100  
101 86 ▶sub _attribute_file {
Subroutine _attribute_file: called CC=3 SCAR=11.0
102 86 my $self = shift;
103 86 my $file = shift;
104  
105 86 my $max = 10;
106 86 ▶ while($max-- && -l $file){
Branch: 1 branch
BranchTrueFalseTotal
if ($max-- and -l $file) 17 86 103
Condition66%2/3
$max-- and -l $file
!ll&&!rl&&r
08617
MC/DC50%1/2
$max-- and -l $file
$max-- -l $file
Truth table66%2/3
A and B
A= $max--B= -l $file
ABresult
0X0
100
111
107 17 $file = readlink $file;
108 }
109
110 86 my ($volume,$dirs,$filename) = File::Spec->splitpath($file);
111 86 return File::Spec->catpath($volume, $dirs, ".$filename.attributes");
112 }
113  
114 __END__
« Attributes.pm ↑ Summary Attributes/Base.pm »