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.0002';
4 12     12   48394 use 5.010;
  12         37  
5              
6 12     12   53 use Scalar::Util qw(blessed);
  12         20  
  12         535  
7              
8 12     12   331 use Class::Tiny::Antlers qw(-default around);
  12         3031  
  12         60  
9 12     12   1821 use namespace::clean;
  12         8774  
  12         58  
10              
11             extends 'Statistics::R::REXP';
12              
13              
14 12     12   2633 use constant sexptype => 'S4SXP';
  12         24  
  12         1319  
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   800 '""' => sub { shift->_to_s };
  12     1   732  
  12         87  
  1         4  
31              
32             sub BUILD {
33 60     60 0 1119 my ($self, $args) = (shift, shift);
34              
35             # Required attribute
36 60 50       166 die "Attribute 'class' is required" unless defined $args->{class};
37            
38             # Required attribute type
39 60 100 66     948 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     2122 grep { defined($_) && ! (blessed($_) && $_->isa('Statistics::R::REXP')) } values(%{$self->slots});
  125 100 66     1176  
  58         1146  
43            
44 58 100 66     989 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   2 my $self = shift;
58              
59             "object of class '" . $self->class . "' (package " . $self->package . ") with " .
60 1         24 scalar(keys(%{$self->slots})) . " slots"
  1         34  
61             }
62              
63             sub to_pl {
64 1     1 1 698 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__