File Coverage

blib/lib/Statistics/R/REXP/S4.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 10 80.0
condition 7 12 58.3
subroutine 10 10 100.0
pod 1 2 50.0
total 56 64 87.5


line stmt bran cond sub pod time code
1             package Statistics::R::REXP::S4;
2             # ABSTRACT: an R closure
3             $Statistics::R::REXP::S4::VERSION = '1.0001';
4 12     12   16113 use 5.010;
  12         29  
5              
6 12     12   39 use Scalar::Util qw(blessed);
  12         11  
  12         517  
7              
8 12     12   479 use Class::Tiny::Antlers qw(-default around);
  12         3953  
  12         55  
9 12     12   1527 use namespace::clean;
  12         9481  
  12         53  
10              
11             extends 'Statistics::R::REXP';
12              
13              
14 12     12   1563 use constant sexptype => 'S4SXP';
  12         14  
  12         1221  
15              
16             has class => (
17             is => 'ro',
18             );
19              
20             has 'package' => (
21             is => 'ro',
22             );
23              
24             has slots => (
25             is => 'ro',
26             default => sub { {} },
27             );
28              
29             use overload
30 12     12   933 '""' => sub { shift->_to_s };
  12     1   696  
  12         83  
  1         3  
31              
32             sub BUILD {
33 60     60 0 1034 my ($self, $args) = (shift, shift);
34              
35             # Required attribute
36 60 50       171 die "Attribute 'class' is required" unless defined $args->{class};
37            
38             # Required attribute type
39 60 100 66     998 die "Attribute 'class' must be a scalar value" unless defined($self->class) && !ref($self->class);
40            
41             die "Attribute 'slots' must be a reference to a hash of REXPs or undefs" if ref($self->slots) ne 'HASH' ||
42 59 50 33     2132 grep { defined($_) && ! (blessed($_) && $_->isa('Statistics::R::REXP')) } values(%{$self->slots});
  125 100 66     1232  
  58         1124  
43            
44 58 100 66     1004 die "Attribute 'package' must be a scalar value" unless defined($self->package) && !ref($self->package);
45             }
46              
47             around _eq => sub {
48             my $orig = shift;
49             return unless $orig->(@_);
50             my ($self, $obj) = (shift, shift);
51             Statistics::R::REXP::_compare_deeply($self->class, $obj->class) &&
52             Statistics::R::REXP::_compare_deeply($self->slots, $obj->slots) &&
53             Statistics::R::REXP::_compare_deeply($self->package, $obj->package)
54             };
55              
56             sub _to_s {
57 1     1   1 my $self = shift;
58              
59             "object of class '" . $self->class . "' (package " . $self->package . ") with " .
60 1         23 scalar(keys(%{$self->slots})) . " slots"
  1         33  
61             }
62              
63             sub to_pl {
64 1     1 1 449 my $self = shift;
65            
66 1         23 { class => $self->class, slots => $self->slots, package => $self->package }
67             }
68              
69             1; # End of Statistics::R::REXP::S4
70              
71             __END__