File Coverage

blib/lib/Statistics/R/REXP/Vector.pm
Criterion Covered Total %
statement 38 39 97.4
branch 17 20 85.0
condition 7 9 77.7
subroutine 12 12 100.0
pod 2 4 50.0
total 76 84 90.4


line stmt bran cond sub pod time code
1             package Statistics::R::REXP::Vector;
2             # ABSTRACT: an R vector
3             $Statistics::R::REXP::Vector::VERSION = '1.0002';
4 25     25   56196 use 5.010;
  25         105  
5              
6 25     25   133 use Scalar::Util qw(blessed);
  25         50  
  25         1170  
7              
8 25     25   481 use Class::Tiny::Antlers qw(-default around);
  25         3358  
  25         136  
9              
10             extends 'Statistics::R::REXP';
11              
12 25     25   4776 use overload '""' => sub { shift->_to_s; };
  25     1116   776  
  25         172  
  1116         14475  
13              
14             has type => (
15             is => 'ro',
16             default => sub { shift->_type; },
17             );
18              
19             has elements => (
20             is => 'ro',
21             default => sub { []; },
22             );
23              
24              
25             sub BUILDARGS {
26 9557     9557 0 56614 my $class = shift;
27 9557 100       30437 if ( scalar @_ == 1 ) {
    100          
28 2852 50 66     10850 if ( ref $_[0] eq 'HASH' ) {
    100          
29 0         0 return $_[0];
30             }
31             elsif (blessed($_[0]) && $_[0]->isa('Statistics::R::REXP::Vector')) {
32 18         383 return { elements => $_[0]->elements }
33             } else {
34 2834         8413 return { elements => $_[0] }
35             }
36             }
37             elsif ( @_ % 2 ) {
38 9         72 die "The new() method for $class expects a hash reference or a key/value list."
39             . " You passed an odd number of arguments\n";
40             }
41             else {
42 6696         20962 return { @_ };
43             }
44             }
45              
46              
47             sub BUILD {
48 9539     9539 0 106983 my ($self, $args) = @_;
49              
50 9539 50       22863 die "This is an abstract class and must be subclassed" if ref($self) eq __PACKAGE__;
51              
52             # Required methods
53 9539         17061 for my $req ( qw/_type/ ) {
54 9539 50       35041 die "$req method required" unless $self->can($req);
55             }
56            
57             # Required attribute type
58 9539 100 66     154847 die "Attribute 'elements' must be an array reference" if defined $self->elements &&
59             ref($self->elements) ne 'ARRAY'
60             }
61              
62              
63             around _eq => sub {
64             my $orig = shift;
65              
66             return undef unless $orig->(@_);
67              
68             my ($self, $obj) = (shift, shift);
69              
70             Statistics::R::REXP::_compare_deeply($self->elements, $obj->elements)
71             };
72              
73              
74             sub _to_s {
75 838     838   1341 my $self = shift;
76 838 100   838   2637 my $stringify = sub { map { defined $_ ? $_ : 'undef'} @_ };
  838         4828  
  2418         12810  
77 838         2369 $self->_type . '(' . join(', ', &$stringify(@{$self->elements})) . ')';
  838         13453  
78             }
79              
80              
81             ## Turns any references (nested lists) into a plain-old flat list.
82             ## Lists can nest to an arbitrary level, but having references to
83             ## anything other than arrays is not supported.
84             sub _flatten {
85 7591 100   7591   15201 map { ref $_ eq 'ARRAY' ? _flatten(@{$_}) : $_ } @_
  31834         66692  
  16         50  
86             }
87              
88             sub is_vector {
89 10     10 1 944 return 1;
90             }
91              
92              
93             sub to_pl {
94 45     45 1 5819 my $self = shift;
95 100 100 100     743 [ map { (blessed $_ && $_->can('to_pl')) ?
96             $_->to_pl : $_ }
97 45         75 @{$self->elements} ]
  45         903  
98             }
99              
100             1; # End of Statistics::R::REXP::Vector
101              
102             __END__