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.0001';
4 25     25   25168 use 5.010;
  25         92  
5              
6 25     25   85 use Scalar::Util qw(blessed);
  25         29  
  25         1068  
7              
8 25     25   519 use Class::Tiny::Antlers qw(-default around);
  25         4006  
  25         117  
9              
10             extends 'Statistics::R::REXP';
11              
12 25     25   3798 use overload '""' => sub { shift->_to_s; };
  25     1113   724  
  25         148  
  1113         11898  
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 319400 my $class = shift;
27 9557 100       20336 if ( scalar @_ == 1 ) {
    100          
28 2852 50 66     9189 if ( ref $_[0] eq 'HASH' ) {
    100          
29 0         0 return $_[0];
30             }
31             elsif (blessed($_[0]) && $_[0]->isa('Statistics::R::REXP::Vector')) {
32 18         365 return { elements => $_[0]->elements }
33             } else {
34 2834         6576 return { elements => $_[0] }
35             }
36             }
37             elsif ( @_ % 2 ) {
38 9         70 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         15851 return { @_ };
43             }
44             }
45              
46              
47             sub BUILD {
48 9539     9539 0 85942 my ($self, $args) = @_;
49              
50 9539 50       15596 die "This is an abstract class and must be subclassed" if ref($self) eq __PACKAGE__;
51              
52             # Required methods
53 9539         10357 for my $req ( qw/_type/ ) {
54 9539 50       25172 die "$req method required" unless $self->can($req);
55             }
56            
57             # Required attribute type
58 9539 100 66     134469 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 835     835   786 my $self = shift;
76 835 100   835   1787 my $stringify = sub { map { defined $_ ? $_ : 'undef'} @_ };
  835         3150  
  2412         10012  
77 835         1891 $self->_type . '(' . join(', ', &$stringify(@{$self->elements})) . ')';
  835         12209  
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   8572 map { ref $_ eq 'ARRAY' ? _flatten(@{$_}) : $_ } @_
  31834         46832  
  16         40  
86             }
87              
88             sub is_vector {
89 10     10 1 743 return 1;
90             }
91              
92              
93             sub to_pl {
94 45     45 1 4454 my $self = shift;
95 100 100 100     633 [ map { (blessed $_ && $_->can('to_pl')) ?
96             $_->to_pl : $_ }
97 45         44 @{$self->elements} ]
  45         856  
98             }
99              
100             1; # End of Statistics::R::REXP::Vector
101              
102             __END__