OFC2/PieValues.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« OFC2/Pie.pm ↑ Summary OFC2/Scatter.pm »
1 package Chart::OFC2::PieValues;
2  
3 =head1 NAME
4  
5 Chart::OFC2::PieValues - OFC2 values for pie charts object
6  
7 =head1 SYNOPSIS
8  
9 =head1 DESCRIPTION
10  
11 =cut
12  
13 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
14 use Moose::Util::TypeConstraints;
15 use MooseX::StrictConstructor;
16  
17 use Carp::Clan 'croak';
18 use List::MoreUtils 'any';
19  
20 our $VERSION = '0.07';
21  
22 coerce 'Chart::OFC2::PieValues'
23 => from 'ArrayRef'
24 => via { Chart::OFC2::PieValues->_new_from_arrayref($_) };
25  
26 =head1 PROPERTIES
27  
28 has 'values' => ( is => 'rw', isa => 'ArrayRef', );
29 has 'texts' => ( is => 'rw', isa => 'ArrayRef', );
30  
31 =cut
32  
33 has 'values' => ( is => 'rw', isa => 'ArrayRef', 'required' => 1);
34 has 'labels' => ( is => 'rw', isa => 'ArrayRef', );
35 has 'colours' => ( is => 'rw', isa => 'ArrayRef', );
36  
37  
38 =head1 METHODS
39  
40 =head2 new()
41  
42 Object constructor.
43  
44 =head1 _new_from_arrayref
45  
46 Allow object creation by coerce of ArrayRef.
47  
48 =cut
49  
50 sub _new_from_arrayref {
51 my $class = shift;
52 my $arrayref_values = shift;
53
54 croak 'pass ArrayRef as argument'
55 if not ref $arrayref_values ne 'ArrayRef';
56
57 my (@values, @labels, @colours);
58 foreach my $value (@{$arrayref_values}) {
59 if (ref $value eq 'HASH') {
60 push @values, $value->{'value'};
61 push @labels, $value->{'label'};
62 push @colours, $value->{'colour'};
63 }
64 else {
65 push @values, $value;
66 push @labels, undef;
67 push @colours, undef;
68 }
69 }
70
71 return $class->new(
72 'values' => \@values,
73 'labels' => \@labels,
74 ((any { defined $_ } @colours) ? ('colours' => \@colours) : ()),
75 );
76 }
77  
78 =head2 TO_JSON()
79  
80 Returns HashRef that is possible to give to C<encode_json()> function.
81  
82 =cut
83  
84 sub TO_JSON {
85 my ($self) = @_;
86
87 my @values_with_labels;
88 my @values = @{$self->values};
89 my @labels = @{$self->labels};
90 for (my $i = 0; $i < @values; $i++) {
91 push @values_with_labels, (
92 defined $labels[$i]
93 ? { 'value' => $values[$i], 'label' => $labels[$i] }
94 : $values[$i]
95 );
96 }
97
98 return \@values_with_labels;
99 }
100  
101 __PACKAGE__->meta->make_immutable;
102  
103 1;
104  
105  
106 __END__
« OFC2/Pie.pm ↑ Summary OFC2/Scatter.pm »