| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Elive::Util::Type; | 
| 2 | 36 |  |  | 36 |  | 178 | use warnings; use strict; | 
|  | 36 |  |  | 36 |  | 56 |  | 
|  | 36 |  |  |  |  | 1154 |  | 
|  | 36 |  |  |  |  | 145 |  | 
|  | 36 |  |  |  |  | 51 |  | 
|  | 36 |  |  |  |  | 861 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 36 |  |  | 36 |  | 573 | use Mouse; | 
|  | 36 |  |  |  |  | 24685 |  | 
|  | 36 |  |  |  |  | 284 |  | 
| 5 | 36 |  |  | 36 |  | 13291 | use Mouse::Util::TypeConstraints; | 
|  | 36 |  |  |  |  | 63 |  | 
|  | 36 |  |  |  |  | 261 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | =head1 NAME | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | Elive::Util::Type - Type introspection class | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =cut | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has 'type' => (is => 'rw', isa => 'Str', required => 1); | 
| 16 |  |  |  |  |  |  | has 'array_type' => (is => 'rw', isa => 'Str'); | 
| 17 |  |  |  |  |  |  | has 'elemental_type' => (is => 'rw', isa => 'Str', required => 1); | 
| 18 |  |  |  |  |  |  | has '_other_types' => (is => 'rw', isa => 'ArrayRef[Str]', required => 1); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub BUILDARGS { | 
| 21 | 58 |  |  | 58 | 1 | 283 | my ($class, $type) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 58 |  |  |  |  | 66 | my %info; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # | 
| 26 |  |  |  |  |  |  | # Bit of a hack, only works on Elive specific types and type unions. | 
| 27 |  |  |  |  |  |  | # | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my $array_type; | 
| 30 | 58 |  |  |  |  | 163 | my ($elemental_type, @other_types) = split(/\|/, $type); | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 58 | 100 |  |  |  | 248 | if ($type =~ m{^Elive::}) { | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 39 | 100 |  |  |  | 343 | if ($type->can('element_class')) { | 
| 35 | 35 |  | 100 |  |  | 123 | ($elemental_type, @other_types) = split(/\|/, $type->element_class || 'Str'); | 
| 36 | 35 |  |  |  |  | 354 | $array_type = $type; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 58 |  |  |  |  | 127 | $info{type} = $type; | 
| 41 | 58 | 100 |  |  |  | 141 | $info{array_type} = $array_type if defined $array_type; | 
| 42 | 58 |  |  |  |  | 89 | $info{elemental_type} = $elemental_type; | 
| 43 | 58 |  |  |  |  | 90 | $info{_other_types} = \@other_types; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 58 |  |  |  |  | 533 | return \%info; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head1 METHODS | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head2 new | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | $type = Elive::Util::inspect_type('Elive::Entity::Participants'); | 
| 53 |  |  |  |  |  |  | if ($type->is_array) { | 
| 54 |  |  |  |  |  |  | # ... | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | Creates an object of type L. | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =cut | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =head2 is_struct | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | Return true, if the type is an ancestor of Elive::DAO | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | =cut | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub is_struct { | 
| 68 | 27 |  |  | 27 | 1 | 30 | my $self = shift; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 27 |  |  |  |  | 53 | my $elemental_type = $self->elemental_type; | 
| 71 | 27 |  | 66 |  |  | 277 | return ($elemental_type  =~ m{^Elive::} | 
| 72 |  |  |  |  |  |  | && $elemental_type->isa('Elive::DAO')); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head2 is_ref | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Return true if the elemental_type is a reference; including objects. | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =cut | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub is_ref { | 
| 82 | 4 |  |  | 4 | 1 | 6 | my $self = shift; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 4 |  | 66 |  |  | 9 | return $self->is_array || $self->is_struct || $self->elemental_type =~ m{^Ref}x; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head2 is_array | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | Return an elemental class if objects are substantiated as arrays. | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | my $type = Elive::Util::Type->new('Elive::Entity::Participants'); | 
| 92 |  |  |  |  |  |  | print $type->is_array; | 
| 93 |  |  |  |  |  |  | # prints Elive::Entity::Participant | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | If the class is an array, the C and C methods inquire | 
| 96 |  |  |  |  |  |  | on the properties of the element class. | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =cut | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub is_array { | 
| 101 | 8 |  |  | 8 | 1 | 2736 | my $self = shift; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 8 |  |  |  |  | 52 | return $self->array_type; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =head2 elemental_type | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Returns the type of the class. For arrays, returns the array element | 
| 109 |  |  |  |  |  |  | class. | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =cut | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 union | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Return the full type union.  For arrays, returns the union of all possible | 
| 116 |  |  |  |  |  |  | array element classes. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | my @types = Elive::Util::Type->new(' Elive::Entity::Group::Members')->union; | 
| 119 |  |  |  |  |  |  | # | 
| 120 |  |  |  |  |  |  | # group members may contain sub-groups, user objects, or packed strings | 
| 121 |  |  |  |  |  |  | is(\@types, [qw(Elive::Entity::Group Str)]); | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =cut | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub union { | 
| 126 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 0 |  |  |  |  |  | return ($self->elemental_type, @{ $self->_other_types }); | 
|  | 0 |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | 1; |