File Coverage

blib/lib/Statistics/R/REXP.pm
Criterion Covered Total %
statement 47 47 100.0
branch 32 34 94.1
condition 19 24 79.1
subroutine 13 13 100.0
pod 3 4 75.0
total 114 122 93.4


line stmt bran cond sub pod time code
1             package Statistics::R::REXP;
2             # ABSTRACT: base class for R objects (Cs)
3             $Statistics::R::REXP::VERSION = '1.0001';
4 34     34   28441 use 5.010;
  34         68  
5              
6 34     34   115 use Scalar::Util qw( blessed );
  34         30  
  34         1333  
7              
8 34     34   538 use Class::Tiny::Antlers;
  34         4093  
  34         157  
9              
10             has attributes => (
11             is => 'ro',
12             );
13              
14             use overload
15 7232     7232   398106 eq => sub { shift->_eq(@_) },
16 34     34   5555 ne => sub { ! shift->_eq(@_) };
  34     52   742  
  34         242  
  52         15488  
17              
18              
19             sub BUILD {
20 15495     15495 0 292557 my ($self, $args) = @_;
21              
22 15495 100       26145 die "This is an abstract class and must be subclassed" if ref($self) eq __PACKAGE__;
23              
24             # Required methods
25 15494         15838 for my $req ( qw/sexptype to_pl/ ) {
26 30987 100       79064 die "$req method required" unless $self->can($req);
27             }
28            
29             # Required attribute type
30 15493 100 100     272034 die "Attribute 'attributes' must be a hash reference" if defined $self->attributes &&
31             ref($self->attributes) ne 'HASH'
32             }
33              
34             sub _eq {
35 7284     7284   27058 my ($self, $obj) = (shift, shift);
36 7284 100       7830 return undef unless _mutual_isa($self, $obj);
37            
38 7268         98456 my $a = $self->attributes;
39 7268         102273 my $b = $obj->attributes;
40              
41 7268         17224 _compare_deeply($a, $b)
42             }
43              
44              
45             ## Returns true if either argument is a subclass of the other
46             sub _mutual_isa {
47 27889     27889   22066 my ($a, $b) = (shift, shift);
48            
49 27889 100 66     81495 ref $a eq ref $b ||
      66        
      66        
50             (blessed($a) && blessed($b) &&
51             ($a->isa(ref $b) ||
52             $b->isa(ref $a)))
53             }
54              
55              
56             sub _compare_deeply {
57 27109 50   27109   56569 my ($a, $b) = @_ or die 'Need two arguments';
58 27109 100 100     60179 if (defined($a) and defined($b)) {
59 20605 100       18169 return 0 unless _mutual_isa($a, $b);
60 20604 100       38554 if (ref $a eq ref []) {
    100          
61 4538 100       6688 return undef unless scalar(@$a) == scalar(@$b);
62 4536         4085 for (my $i = 0; $i < scalar(@{$a}); $i++) {
  16771         23594  
63 12264 100       15011 return undef unless _compare_deeply($a->[$i], $b->[$i]);
64             }
65             } elsif (ref $a eq ref {}) {
66 1282 50       3116 return undef unless scalar(keys %$a) == scalar(keys %$b);
67 1282         2623 foreach my $name (keys %$a) {
68             return undef unless exists $b->{$name} &&
69 2485 100 66     5932 _compare_deeply($a->{$name}, $b->{$name});
70             }
71             } else {
72 14784 100       20094 return undef unless $a eq $b;
73             }
74             } else {
75 6504 100 100     20299 return undef if defined($a) or defined($b);
76             }
77              
78 27023         150065 return 1;
79             }
80              
81              
82             sub is_null {
83 263     263 1 10146 return 0;
84             }
85              
86              
87             sub is_vector {
88 9     9 1 52 return 0;
89             }
90              
91              
92             sub inherits {
93 4     4 1 552 my ($self, $class) = @_;
94 4         74 my $attributes = $self->attributes;
95 4 100 66     24 return unless $attributes && $attributes->{'class'};
96            
97 3         3 grep {/^$class$/} @{$attributes->{'class'}->to_pl}
  6         64  
  3         7  
98             }
99              
100             1; # End of Statistics::R::REXP
101              
102             __END__