OFC2/HBarValues.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/HBar.pm ↑ Summary OFC2/Labels.pm »
1 package Chart::OFC2::HBarValues;
2  
3 =head1 NAME
4  
5 Chart::OFC2::HBarValues - OFC2 values for horizontal bar 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::HBarValues'
23 => from 'ArrayRef'
24 => via { Chart::OFC2::HBarValues->_new_from_arrayref( $_) };
25  
26 =head1 PROPERTIES
27  
28 has 'values' => ( is => 'rw', isa => 'ArrayRef', );
29  
30 =cut
31  
32 has 'values' => ( is => 'rw', isa => 'ArrayRef', 'required' => 1);
33  
34  
35 =head1 METHODS
36  
37 =head2 new()
38  
39 Object constructor.
40  
41 =head1 _new_from_arrayref
42  
43 Allow object creation by coerce of ArrayRef.
44  
45 =cut
46  
47 sub _new_from_arrayref {
48 my $class = shift;
49 my $arrayref_values = shift;
50
51 croak 'pass ArrayRef as argument'
52 if not ref $arrayref_values ne 'ArrayRef';
53
54 my @values;
55 foreach my $value (@{$arrayref_values}) {
56 if (ref $value eq 'HASH') {
57 push @values, $value;
58 }
59 else {
60 push @values, { 'right' => $value };
61 }
62 }
63
64 return $class->new(
65 'values' => \@values,
66 );
67 }
68  
69 =head2 TO_JSON()
70  
71 Returns ArrayRef that is possible to give to C<encode_json()> function.
72  
73 NOTE: values are reversed. This is done so that the C<y_axis->labels> match to
74 the values properly.
75  
76 =cut
77  
78 sub TO_JSON {
79 my $self = shift;
80
81 return [ reverse @{$self->values} ];
82 }
83  
84 __PACKAGE__->meta->make_immutable;
85  
86 1;
87  
88  
89 __END__
« OFC2/HBar.pm ↑ Summary OFC2/Labels.pm »