| 1 | package PDL::Role::HasNames; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Role for attaching per-element names to a piddle | ||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | our $VERSION = '0.002003'; # VERSION | |||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Role::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use PDL::SV (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Hash::Util::FieldHash qw(fieldhash); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use Safe::Isa; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use Type::Params; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Types::Standard qw(ConsumerOf ArrayRef); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | fieldhash my %objects; | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 31 | ▶ | sub names { | ||||||||
|
Subroutine
names: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 23 | 31 | my ( $self, $names ) = @_; | |||||||||
| 24 | |||||||||||
| 25 | 31 | ▶ | if ( defined $names ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | state $check = Type::Params::compile( ( ConsumerOf ['PDL::SV'] ) | ||||||||||
| 27 | 16 | ▶ | ->plus_coercions( ArrayRef, sub { PDL::SV->new($_) } ) ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 16 | ($names) = $check->($names); | |||||||||
| 29 | |||||||||||
| 30 | 16 | ▶ | unless ( ( $names->shape == $self->shape )->all ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | die "names has to be of same length as the raw piddle"; | |||||||||
| 32 | } | ||||||||||
| 33 | 16 | $objects{$self}{names} = $names; | |||||||||
| 34 | } | ||||||||||
| 35 | 31 | return $objects{$self}{names}; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 6 | ▶ | sub _around_and_attach_names { | ||||||||
|
Subroutine
_around_and_attach_names: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 6 | my ($f_new_names) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 8 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||
| 42 | 8 | my $orig = shift; | |||||||||
| 43 | 8 | my $self = shift; | |||||||||
| 44 | |||||||||||
| 45 | 8 | my $new = $self->$orig(@_); | |||||||||
| 46 | 8 | ▶ | if ( defined (my $names = $self->names) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 5 | ▶ | unless ( $new->$_DOES(__PACKAGE__) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | Role::Tiny->apply_roles_to_object( $new, __PACKAGE__ ); | |||||||||
| 49 | } | ||||||||||
| 50 | 5 | $new->names( $names->$f_new_names(@_) ); | |||||||||
| 51 | } | ||||||||||
| 52 | 8 | return $new; | |||||||||
| 53 | 6 | }; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 2 | for my $method (qw(copy setbadif)) { | |||||||||
| 57 | 5 | ▶ | around $method => _around_and_attach_names( sub { shift->copy; } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 58 | } | ||||||||||
| 59 | 0 | ▶ | around slice => _around_and_attach_names( sub { shift->slice(@_); } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 60 | |||||||||||
| 61 | 2 | 1; | |||||||||
| 62 | |||||||||||
| 63 | __END__ | ||||||||||