lib/Data/CloudWeights.pm

filter: Statementstmt 97.1% 170 / 175 Branchbran 66.6% 48 / 72 Conditioncond 50.0% 10 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 92.8% 26 / 28 Podpod 100% 2 / 2 totaltotal 83.2% 258 / 310 CC 45 SCAR 20.6
CC
45
Cov
85%
__ANON__
19.3
add
11.0
formation
8.1
CRAP 7.9
↑ Summary
1 package Data::CloudWeights;
2  
3 1 ▶use 5.01;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use version; our $VERSION = qv( sprintf '0.15.%d', q$Rev: 1 $ =~ /\d+/gmx );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Type::Utils qw( as enum message subtype where );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Types::Standard qw( ArrayRef HashRef Int Maybe Num Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 my $COLOUR = subtype 'Colour', as Str,
13 14 ▶ where { not $_ or $_ =~ m{ \A \# [0-9A-Fa-f]+ \z }mx };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
not $_ or $_ =~ / \A \# [0-9A-Fa-f]+ \z /mx 0 14 14
14 1 my $SORT_ORDER = enum 'Sort_Order' => [ qw( asc desc ) ];
15 1 my $SORT_TYPE = enum 'Sort_Type' => [ qw( alpha numeric ) ];
16  
17 # Private functions
18 2 ▶my $_rgb2hsi = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
19 2 my ( $r, $g, $b ) = @_; my ( $h, $s, $i ) = ( 0, 0, 0 );
20  
21 2 ▶ $i = ( $r + $g + $b ) / 3; $i == 0 and return ( $h, $s, $i );
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 and return $h, $s, $i 0 2 2
22  
23 2 my $x = $r - 0.5 * ( $g + $b ); my $y = 0.866025403 * ( $g - $b );
24  
25 2 ▶ $s = ( $x ** 2 + $y ** 2 ) ** 0.5; $s == 0 and return ( $h, $s, $i );
Branch: 1 branch
BranchTrueFalseTotal
$s == 0 and return $h, $s, $i 0 2 2
26  
27 2 $h = POSIX::atan2( $y , $x ) / ( 2 * 3.1415926535 );
28  
29 2 return ( $h, $s, $i );
30 1 };
31  
32 10 ▶my $_hsi2rgb = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
33 10 my ( $h, $s, $i ) = @_; my ( $r, $g, $b ) = ( 0, 0, 0 );
34  
35 # Degenerate cases. If !intensity it's black, if !saturation it's grey
36 10 ▶ $i == 0 and return ( $r, $g, $b ); $s == 0 and return ( $i, $i, $i );
Branch: 2 branches
BranchTrueFalseTotal
$i == 0 and return $r, $g, $b 0 10 10
$s == 0 and return $i, $i, $i 0 10 10
37  
38 10 $h = $h * 2 * 3.1415926535;
39  
40 10 my $x = $s * cos( $h ); my $y = $s * sin( $h );
41  
42 10 $r = $i + ( 2 / 3 * $x );
43 10 $g = $i - ( $x / 3 ) + ( $y / 2 / 0.866025403 );
44 10 $b = $i - ( $x / 3 ) - ( $y / 2 / 0.866025403 );
45 # Limit 0<=x<=1 ## YUCK but we go outta range without it.
46 10 ▶ ( $r, $b, $g ) = map { $_ < 0 ? 0 : $_ > 1 ? 1 : $_ } ( $r, $b, $g );
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 10 20 30
$_ > 1 ? : 0 20 20
47  
48 10 return ( $r, $g, $b );
49 1 };
50  
51 1 ▶my $_generate = sub { # Robbed from Color::Spectrum since it's abandoned
Subroutine __ANON__: called CC=15 SCAR=29.6
52 1 ▶ my ($cnt, $col1, $col2) = @_; $col2 ||= $col1; my @murtceps;
Condition33%1/3
$col2 ||= $col1
l!l&&r!l&&!r
100
MC/DC0%0/2
$col2 ||= $col1
$col2 $col1
Truth table33%1/3
A ||= B
A= $col2B= $col1
ABresult
1X1
011
000
53  
54 1 ▶ push @murtceps, uc $col1; my $pound = $col1 =~ /^\#/ ? '#' : q();
Branch: 1 branch
BranchTrueFalseTotal
$col1 =~ /^\#/ ? : 1 0 1
55  
56 1 $col1 =~s/^\#//; $col2 =~s/^\#//;
57  
58 1 ▶ my $clockwise = 0; $cnt < 0 and $clockwise++; $cnt = int( abs( $cnt ) );
Branch: 1 branch
BranchTrueFalseTotal
$cnt < 0 and ++$clockwise 0 1 1
59  
60 1 ▶ $cnt <= 1 and return ( wantarray() ? @murtceps : \@murtceps );
Branch: 2 branches
BranchTrueFalseTotal
$cnt <= 1 and return wantarray ? @murtceps : \@murtceps 0 1 1
wantarray ? : 0 0 0
61 1 ▶ $cnt == 2 and return ( wantarray()
Branch: 2 branches
BranchTrueFalseTotal
$cnt == 2 and return wantarray ? ("$pound$col1", "$pound$col2") : ["$pound$col1", "$pound$col2"] 0 1 1
wantarray ? : 0 0 0
62 ? ( "${pound}${col1}", "${pound}${col2}" )
63 : [ "${pound}${col1}", "${pound}${col2}" ] );
64 # The RGB values need to be on the decimal scale,
65 # so we divide em by 255 enpassant.
66 my ( $h1, $s1, $i1 )
67 1 = $_rgb2hsi->( map { hex() / 255 } unpack( 'a2a2a2', $col1 ) );
68 my ( $h2, $s2, $i2 )
69 1 = $_rgb2hsi->( map { hex() / 255 } unpack( 'a2a2a2', $col2 ) );
70  
71 1 $cnt--; my $sd = ( $s2 - $s1 ) / $cnt; my $id = ( $i2 - $i1 ) / $cnt;
72  
73 1 my $hd = $h2 - $h1;
74  
75 1 ▶ $hd = ( uc( $col1 ) eq uc( $col2 ) )
Branch: 3 branches
BranchTrueFalseTotal
uc $col1 eq uc $col2 ? : 0 1 1
$clockwise ? : 0 0 0
$hd < 0 ? : 0 1 1
76 ? ( $clockwise ? -1 : 1 ) / $cnt
77 : ( ( $hd < 0 ? 1 : 0 ) + $hd - $clockwise) / $cnt;
78  
79 1 ▶ while (--$cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$cnt) 10 1 11
80 10 $s1 += $sd; $i1 += $id; $h1 += $hd;
81 10 ▶ $h1 > 1 and $h1 -= 1; $h1 < 0 and $h1 += 1;
Branch: 2 branches
BranchTrueFalseTotal
$h1 > 1 and $h1 -= 1 0 10 10
$h1 < 0 and $h1 += 1 1 9 10
82 push @murtceps, sprintf "${pound}%02X%02X%02X",
83 10 map { int( $_ * 255 + 0.5 ) } $_hsi2rgb->( $h1, $s1, $i1 );
84 }
85  
86 1 push @murtceps, uc "${pound}${col2}";
87  
88 1 ▶ return wantarray() ? @murtceps : \@murtceps;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
89 1 };
90  
91 # Attribute constructors
92 1 ▶my $_build_colour_pallet = sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
93 1 my $self = shift;
94 # Unsetting hot or cold colour strings in the constructor will cause
95 # the default pallet to be used instead
96 1 ▶ $self->cold_colour and $self->hot_colour
Branch: 1 branch
BranchTrueFalseTotal
$self->cold_colour and $self->hot_colour and return [&$_generate(12, $self->cold_colour, $self->hot_colour)] 1 0 1
Condition33%1/3
$self->cold_colour and $self->hot_colour
!ll&&!rl&&r
001
MC/DC0%0/2
$self->cold_colour and $self->hot_colour
$self->cold_colour $self->hot_colour
Truth table33%1/3
A and B
A= $self->cold_colourB= $self->hot_colour
ABresult
0X0
100
111
97 and return [ $_generate->( 12, $self->cold_colour, $self->hot_colour ) ];
98  
99 0 return [ '#CC33FF', '#663399', '#3300CC', '#99CCFF',
100 '#00FFFF', '#66FFCC', '#66CC99', '#006600',
101 '#CCFF66', '#FFFF33', '#FF6600', '#FF0000' ];
102 1 };
103  
104 # Public attributes
105 1 has 'cold_colour' => is => 'ro', isa => Maybe[$COLOUR],
106 documentation => 'Blue', default => '#0000FF';
107  
108 1 has 'hot_colour' => is => 'ro', isa => Maybe[$COLOUR],
109 documentation => 'Red', default => '#FF0000';
110  
111 1 has 'colour_pallet' => is => 'lazy', isa => ArrayRef[$COLOUR],
112 documentation => 'Alternative to colour calculation',
113 builder => $_build_colour_pallet;
114  
115 1 has 'decimal_places' => is => 'ro', isa => Int, default => 3,
116 documentation => 'Defaults for ems';
117  
118 1 has 'limit' => is => 'rw', isa => Int, default => 0,
119 documentation => 'Max size of returned list. Zero no limit';
120  
121 1 has 'max_count' => is => 'rwp', isa => Int, default => 0,
122 documentation => 'Current max value across all tags cloud';
123  
124 1 has 'max_size' => is => 'rwp', isa => Num, default => 3.0,
125 documentation => 'Output size no more than';
126  
127 1 has 'min_count' => is => 'rwp', isa => Int, default => -1,
128 documentation => 'Current min';
129  
130 1 has 'min_size' => is => 'rwp', isa => Num, default => 1.0,
131 documentation => 'Output size no less than';
132  
133 1 has 'sort_field' => is => 'rw', isa => Maybe[Str], default => 'tag',
134 documentation => 'Output sorted by this field';
135  
136 1 has 'sort_order' => is => 'rw', isa => $SORT_ORDER,
137 documentation => 'Sort order - asc or desc', default => 'asc';
138  
139 1 has 'sort_type' => is => 'rw', isa => $SORT_TYPE,
140 documentation => 'Sort type - alpha or numeric',
141 default => 'alpha';
142  
143 1 has 'total_count' => is => 'rwp', isa => Int, default => 0,
144 documentation => 'Current total for all tags in the cloud';
145  
146 # Private attributes
147 1 ▶has '_index' => is => 'ro', isa => HashRef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
148 1 ▶has '_sorts' => is => 'ro', isa => HashRef, default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.1
149 alpha => {
150 3 ▶ asc => sub { my $x = shift; sub { $_[ 0 ]->{ $x } cmp $_[ 1 ]->{ $x } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
151 3 },
152 2 ▶ desc => sub { my $x = shift; sub { $_[ 1 ]->{ $x } cmp $_[ 0 ]->{ $x } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
153 2 },
154 },
155 numeric => {
156 0 ▶ asc => sub { my $x = shift; sub { $_[ 0 ]->{ $x } <=> $_[ 1 ]->{ $x } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 },
158 1 ▶ desc => sub { my $x = shift; sub { $_[ 1 ]->{ $x } <=> $_[ 0 ]->{ $x } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 },
160 1 } } };
161 1 ▶has '_tags' => is => 'ro', isa => ArrayRef, default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
162  
163 # Private methods
164 7 ▶my $_get_sort_method = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
165 7 my $self = shift; # Add called multiple times, determine the sorting method
166 # No sorting if sort field is false
167 7 ▶ my $field = $self->sort_field or return sub { return 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->sort_field or return sub { return 0; } 6 1 7
168  
169 6 ▶ ref $field and return $field; # User supplied subroutine
Branch: 1 branch
BranchTrueFalseTotal
ref $field and return $field 0 6 6
170  
171 my $orderby = $self->_sorts->{ lc $self->sort_type }
172 6 ->{ lc $self->sort_order }->( $field );
173 # Protect against wrong sort type for the data
174 return $field ne 'tag'
175 2 ▶ ? sub { return $orderby->( @_ ) || $_[ 0 ]->{tag} cmp $_[ 1 ]->{tag} }
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition66%2/3
&$orderby(@_) || $_[0]{'tag'} cmp $_[1]{'tag'}
l!l&&r!l&&!r
110
MC/DC0%0/2
&$orderby(@_) || $_[0]{'tag'} cmp $_[1]{'tag'}
&$orderby(@_) $_[0]{'tag'} cmp $_[1]{'tag'}
Truth table66%2/3
A || B
A= &$orderby(@_)B= $_[0]{'tag'} cmp $_[1]{'tag'}
ABresult
1X1
011
000
176 6 ▶ : $orderby;
Branch: 1 branch
BranchTrueFalseTotal
$field ne 'tag' ? : 1 5 6
177 1 };
178  
179 # Public methods
180 6 ▶sub add { # Include the passed args in this cloud's formation
Subroutine add: called CC=11 SCAR=24.0
Pod documented
181 6 my ($self, $tag, $count, $value) = @_;
182  
183 6 ▶ $tag or return; # Mandatory arg used as a key in tag ref index
Branch: 1 branch
BranchTrueFalseTotal
$tag or return 5 1 6
184  
185 # Mask out null strings and negative numbers from the passed count value
186 5 ▶ $count = defined $count ? abs $count : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $count ? : 4 1 5
187  
188 # Add this count to the total for this cloud
189 5 $self->_set_total_count( $self->total_count + $count );
190  
191 5 ▶ if (not exists $self->_index->{ $tag }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->_index->{$tag}) { } 3 2 5
192 # Create a new tag reference and add to both list and index
193 3 my $tag_ref = { count => $count, tag => $tag, value => $value };
194  
195 3 push @{ $self->_tags }, $self->_index->{ $tag } = $tag_ref;
196 }
197 else {
198 2 my $index = $self->_index->{ $tag };
199  
200 # Calls with the same tag are cumulative
201 2 $count += $index->{count}; $index->{count} = $count;
202  
203 2 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 0 2
204 2 my $tag_value = $index->{value};
205  
206 # Make an array if there are two or more calls to add the same tag
207 $tag_value and ref $tag_value ne 'ARRAY'
208 2 ▶ and $index->{value} = [ $tag_value ];
Branch: 1 branch
BranchTrueFalseTotal
$tag_value and ref $tag_value ne "ARRAY" and $index->{"value"} = [$tag_value] 1 1 2
Condition66%2/3
$tag_value and ref $tag_value ne "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
$tag_value and ref $tag_value ne "ARRAY"
$tag_value ref $tag_value ne "ARRAY"
Truth table66%2/3
A and B
A= $tag_valueB= ref $tag_value ne "ARRAY"
ABresult
0X0
100
111
209  
210 # Push passed value in each call onto the values array.
211 2 ▶ if ($tag_value) { push @{ $index->{value} }, $value }
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_value) { } 1 1 2
212 1 else { $index->{value} = $value }
213 }
214 }
215  
216 # Update this cloud's max and min values
217 5 ▶ $count > $self->max_count and $self->_set_max_count( $count );
Branch: 1 branch
BranchTrueFalseTotal
$count > $self->max_count and $self->_set_max_count($count) 2 3 5
218 5 ▶ $self->min_count == -1 and $self->_set_min_count( $count );
Branch: 1 branch
BranchTrueFalseTotal
$self->min_count == -1 and $self->_set_min_count($count) 1 4 5
219 5 ▶ $count < $self->min_count and $self->_set_min_count( $count );
Branch: 1 branch
BranchTrueFalseTotal
$count < $self->min_count and $self->_set_min_count($count) 0 5 5
220  
221 # Return the current cumulative count for this tag
222 5 return $count;
223 }
224  
225 7 ▶sub formation { # Calculate the result set for this cloud
Subroutine formation: called CC=8 SCAR=20.9
Pod documented
226 7 my $self = shift;
227 7 my $prec = 10**$self->decimal_places;
228 7 my $bands = scalar @{ $self->colour_pallet } - 1;
229 7 ▶ my $range = (abs $self->max_count - $self->min_count) || 1;
Condition50%1/2
abs $self->max_count - $self->min_count || 1
l!l
70
MC/DC0%0/1
abs $self->max_count - $self->min_count || 1
abs $self->max_count - $self->min_count
Truth table50%1/2
A
A= abs $self->max_count - $self->min_count
Aresult
11
00
230 7 my $step = ($self->max_size - $self->min_size) / $range;
231 7 my $compare = $self->$_get_sort_method;
232 7 my $ntags = @{ $self->_tags };
233 7 my $out = [];
234  
235 7 ▶ $ntags == 0 and return []; # No calls to add were made
Branch: 1 branch
BranchTrueFalseTotal
$ntags == 0 and return [] 1 6 7
236  
237 6 ▶ if ($ntags == 1) { # One call to add was made
Branch: 1 branch
BranchTrueFalseTotal
if ($ntags == 1) 1 5 6
238 0 return [ { colour => $self->hot_colour || pop @{ $self->colour_pallet },
239 count => $self->_tags->[ 0 ]->{count},
240 percent => 100,
241 size => $self->max_size,
242 tag => $self->_tags->[ 0 ]->{tag},
243 1 ▶ value => $self->_tags->[ 0 ]->{value} } ];
Condition33%1/3
$self->hot_colour || pop @{$self->colour_pallet;}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->hot_colour || pop @{$self->colour_pallet;}
$self->hot_colour pop @{$self->colour_pallet;}
Truth table33%1/3
A || B
A= $self->hot_colourB= pop @{$self->colour_pallet;}
ABresult
1X1
011
000
244 }
245  
246 5 for (sort { $compare->( $a, $b ) } @{ $self->_tags }) {
247 11 my $count = $_->{count};
248 11 my $base = $count - $self->min_count;
249 11 my $index = int 0.5 + ($base * $bands / $range);
250 11 my $percent = 100 * $count / $self->total_count;
251 11 my $size = $self->min_size + $step * $base;
252  
253 # Push the return array with a hash ref for each key value pair
254 # passed to the add method
255 11 push @{ $out }, { colour => $self->colour_pallet->[ $index ],
256 count => $count,
257 percent => (int 0.5 + $prec * $percent) / $prec,
258 size => (int 0.5 + $prec * $size ) / $prec,
259 tag => $_->{tag},
260 11 value => $_->{value} };
261  
262 11 ▶ $self->limit and @{ $out } == $self->limit and last;
Branch: 1 branch
BranchTrueFalseTotal
$self->limit and @{$out;} == $self->limit and last 1 10 11
Condition66%2/3
$self->limit and @{$out;} == $self->limit
!ll&&!rl&&r
1001
MC/DC50%1/2
$self->limit and @{$out;} == $self->limit
$self->limit @{$out;} == $self->limit
Truth table66%2/3
A and B
A= $self->limitB= @{$out;} == $self->limit
ABresult
0X0
100
111
263 }
264  
265 5 return $out;
266 }
267  
268 1 1;
269  
270 __END__
↑ Summary