| 1 | package Elive::Util::Type; | ||||||||||||||||||||||||||||||||||
| 2 | 37 | ▶ | use warnings; use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | 37 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 37 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 37 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 37 | our $VERSION = '1.36'; | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | Elive::Util::Type - Type introspection class | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 37 | has 'type' => (is => 'rw', isa => 'Str', required => 1); | |||||||||||||||||||||||||||||||||
| 17 | 37 | has 'array_type' => (is => 'rw', isa => 'Str'); | |||||||||||||||||||||||||||||||||
| 18 | 37 | has 'elemental_type' => (is => 'rw', isa => 'Str', required => 1); | |||||||||||||||||||||||||||||||||
| 19 | 37 | has '_other_types' => (is => 'rw', isa => 'ArrayRef[Str]', required => 1); | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||
| 22 | 3059 | ▶ | my ($class, $type) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 3059 | my %info; | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||||||||
| 27 | # Bit of a hack, tailored for Elive specific types and type unions. | ||||||||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | my $array_type; | ||||||||||||||||||||||||||||||||||
| 31 | 3059 | my ($elemental_type, @other_types) = split(/\|/, $type); | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 3059 | ▶ | if (try { $type->can('element_class') }) { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 34 | 149 | ▶ | ($elemental_type, @other_types) = split(/\|/, $type->element_class || 'Str'); | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$type->element_class || 'Str'
MC/DC100%1/1
$type->element_class || 'Str'
$type->element_class
Truth table100%2/2
A
A= $type->element_class
| |||||||||||||||||||||||||||||||||||
| 35 | 149 | $array_type = $type; | |||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 3059 | $info{type} = $type; | |||||||||||||||||||||||||||||||||
| 39 | 3059 | ▶ | $info{array_type} = $array_type if defined $array_type; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 40 | 3059 | $info{elemental_type} = $elemental_type; | |||||||||||||||||||||||||||||||||
| 41 | 3059 | $info{_other_types} = \@other_types; | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 3059 | return \%info; | |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | =head2 new | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | $type = Elive::Util::inspect_type('Elive::Entity::Participants'); | ||||||||||||||||||||||||||||||||||
| 51 | if ($type->is_array) { | ||||||||||||||||||||||||||||||||||
| 52 | # ... | ||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | Creates an object of type L<Elive::Util::Type>. | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | =head2 is_struct | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | Return true, if the type is an ancestor of Elive::DAO | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | sub is_struct { | ||||||||||||||||||||||||||||||||||
| 66 | 2010 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_struct: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | 2010 | my $elemental_type = $self->elemental_type; | |||||||||||||||||||||||||||||||||
| 69 | 2010 | ▶ | return ($elemental_type =~ m{^Elive::} | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$elemental_type =~ /^Elive::/ && $elemental_type->isa('Elive::DAO')
MC/DC50%1/2
$elemental_type =~ /^Elive::/ && $elemental_type->isa('Elive::DAO')
$elemental_type =~ /^Elive::/
$elemental_type->isa('Elive::DAO')
Truth table66%2/3
A && B
A= $elemental_type =~ /^Elive::/B= $elemental_type->isa('Elive::DAO')
| |||||||||||||||||||||||||||||||||||
| 70 | && $elemental_type->isa('Elive::DAO')); | ||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | =head2 is_ref | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | Return true if the elemental_type is a reference; including objects. | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | sub is_ref { | ||||||||||||||||||||||||||||||||||
| 80 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_ref: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 37 | ▶ | return $self->is_array || $self->is_struct || $self->elemental_type =~ m{^Ref}x; | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
$self->is_array || $self->is_struct || $self->elemental_type =~ /^Ref/x
$self->is_array or $self->is_struct
MC/DC66%2/3
$self->is_array || $self->is_struct || $self->elemental_type =~ /^Ref/x
$self->is_array
$self->is_struct
$self->elemental_type =~ /^Ref/x
Truth table75%3/4
A or B || C
A= $self->is_arrayB= $self->is_structC= $self->elemental_type =~ /^Ref/x
| |||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | =head2 is_array | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | Return an elemental class if objects are substantiated as arrays. | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | my $type = Elive::Util::Type->new('Elive::Entity::Participants'); | ||||||||||||||||||||||||||||||||||
| 90 | print $type->is_array; | ||||||||||||||||||||||||||||||||||
| 91 | # prints Elive::Entity::Participant | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | If the class is an array, the C<is_struct()> and C<is_ref()> methods inquire | ||||||||||||||||||||||||||||||||||
| 94 | on the properties of the element class. | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | sub is_array { | ||||||||||||||||||||||||||||||||||
| 99 | 1922 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 1922 | return $self->array_type; | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | =head2 elemental_type | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | Returns the type of the class. For arrays, returns the array element | ||||||||||||||||||||||||||||||||||
| 107 | class. | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | =head2 union | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | Return the full type union. For arrays, returns the union of all possible | ||||||||||||||||||||||||||||||||||
| 114 | array element classes. | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | my @types = Elive::Util::Type->new(' Elive::Entity::Group::Members')->union; | ||||||||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||||||||
| 118 | # group members may contain sub-groups, user objects, or packed strings | ||||||||||||||||||||||||||||||||||
| 119 | is(\@types, [qw(Elive::Entity::Group Str)]); | ||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | sub union { | ||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
union: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 0 | return ($self->elemental_type, @{ $self->_other_types }); | |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 37 | 1; | |||||||||||||||||||||||||||||||||