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.0';
4 23     23   24291 use 5.010;
  23         90  
5              
6 23     23   79 use Scalar::Util qw(blessed);
  23         28  
  23         1029  
7              
8 23     23   519 use Class::Tiny::Antlers qw(-default around);
  23         4045  
  23         110  
9              
10             extends 'Statistics::R::REXP';
11              
12 23     23   3574 use overload '""' => sub { shift->_to_s; };
  23     921   718  
  23         138  
  921         12477  
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 5700     5700 0 332420 my $class = shift;
27 5700 100       13027 if ( scalar @_ == 1 ) {
    100          
28 2355 50 66     7471 if ( ref $_[0] eq 'HASH' ) {
    100          
29 0         0 return $_[0];
30             }
31             elsif (blessed($_[0]) && $_[0]->isa('Statistics::R::REXP::Vector')) {
32 18         380 return { elements => $_[0]->elements }
33             } else {
34 2337         5394 return { elements => $_[0] }
35             }
36             }
37             elsif ( @_ % 2 ) {
38 9         69 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 3336         8943 return { @_ };
43             }
44             }
45              
46              
47             sub BUILD {
48 5682     5682 0 54057 my ($self, $args) = @_;
49              
50 5682 50       10050 die "This is an abstract class and must be subclassed" if ref($self) eq __PACKAGE__;
51              
52             # Required methods
53 5682         7174 for my $req ( qw/_type/ ) {
54 5682 50       15669 die "$req method required" unless $self->can($req);
55             }
56            
57             # Required attribute type
58 5682 100 66     83876 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 643     643   624 my $self = shift;
76 643 100   643   1365 my $stringify = sub { map { defined $_ ? $_ : 'undef'} @_ };
  643         2518  
  1708         7406  
77 643         1501 $self->_type . '(' . join(', ', &$stringify(@{$self->elements})) . ')';
  643         9679  
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 4487 100   4487   5319 map { ref $_ eq 'ARRAY' ? _flatten(@{$_}) : $_ } @_
  21232         30513  
  16         39  
86             }
87              
88             sub is_vector {
89 10     10 1 722 return 1;
90             }
91              
92              
93             sub to_pl {
94 45     45 1 4986 my $self = shift;
95 100 100 100     618 [ map { (blessed $_ && $_->can('to_pl')) ?
96             $_->to_pl : $_ }
97 45         41 @{$self->elements} ]
  45         833  
98             }
99              
100             1; # End of Statistics::R::REXP::Vector
101              
102             __END__