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.0';
4 32     32   27771 use 5.010;
  32         68  
5              
6 32     32   104 use Scalar::Util qw( blessed );
  32         27  
  32         1221  
7              
8 32     32   542 use Class::Tiny::Antlers;
  32         4055  
  32         143  
9              
10             has attributes => (
11             is => 'ro',
12             );
13              
14             use overload
15 3196     3196   183780 eq => sub { shift->_eq(@_) },
16 32     32   5548 ne => sub { ! shift->_eq(@_) };
  32     52   773  
  32         288  
  52         16242  
17              
18              
19             sub BUILD {
20 8639     8639 0 239036 my ($self, $args) = @_;
21              
22 8639 100       15114 die "This is an abstract class and must be subclassed" if ref($self) eq __PACKAGE__;
23              
24             # Required methods
25 8638         9392 for my $req ( qw/sexptype to_pl/ ) {
26 17275 100       45791 die "$req method required" unless $self->can($req);
27             }
28            
29             # Required attribute type
30 8637 100 100     152869 die "Attribute 'attributes' must be a hash reference" if defined $self->attributes &&
31             ref($self->attributes) ne 'HASH'
32             }
33              
34             sub _eq {
35 3248     3248   12087 my ($self, $obj) = (shift, shift);
36 3248 100       3596 return undef unless _mutual_isa($self, $obj);
37            
38 3232         45505 my $a = $self->attributes;
39 3232         46580 my $b = $obj->attributes;
40              
41 3232         8078 _compare_deeply($a, $b)
42             }
43              
44              
45             ## Returns true if either argument is a subclass of the other
46             sub _mutual_isa {
47 12289     12289   10135 my ($a, $b) = (shift, shift);
48            
49 12289 100 66     37901 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 11949 50   11949   26482 my ($a, $b) = @_ or die 'Need two arguments';
58 11949 100 100     28035 if (defined($a) and defined($b)) {
59 9041 100       8506 return 0 unless _mutual_isa($a, $b);
60 9040 100       17498 if (ref $a eq ref []) {
    100          
61 2022 100       3158 return undef unless scalar(@$a) == scalar(@$b);
62 2020         1855 for (my $i = 0; $i < scalar(@{$a}); $i++) {
  7383         10479  
63 5392 100       6983 return undef unless _compare_deeply($a->[$i], $b->[$i]);
64             }
65             } elsif (ref $a eq ref {}) {
66 558 50       1538 return undef unless scalar(keys %$a) == scalar(keys %$b);
67 558         1222 foreach my $name (keys %$a) {
68             return undef unless exists $b->{$name} &&
69 1077 100 66     2761 _compare_deeply($a->{$name}, $b->{$name});
70             }
71             } else {
72 6460 100       9760 return undef unless $a eq $b;
73             }
74             } else {
75 2908 100 100     9263 return undef if defined($a) or defined($b);
76             }
77              
78 11863         67512 return 1;
79             }
80              
81              
82             sub is_null {
83 263     263 1 10861 return 0;
84             }
85              
86              
87             sub is_vector {
88 9     9 1 57 return 0;
89             }
90              
91              
92             sub inherits {
93 4     4 1 960 my ($self, $class) = @_;
94 4         82 my $attributes = $self->attributes;
95 4 100 66     26 return unless $attributes && $attributes->{'class'};
96            
97 3         5 grep {/^$class$/} @{$attributes->{'class'}->to_pl}
  6         54  
  3         6  
98             }
99              
100             1; # End of Statistics::R::REXP
101              
102             __END__