File Coverage

blib/lib/Elive/Util/Type.pm
Criterion Covered Total %
statement 31 34 91.1
branch 6 6 100.0
condition 6 8 75.0
subroutine 8 9 88.8
pod 5 5 100.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             package Elive::Util::Type;
2 36     36   228 use warnings; use strict;
  36     36   83  
  36         1393  
  36         201  
  36         78  
  36         1028  
3              
4 36     36   1207 use Mouse;
  36         36020  
  36         369  
5 36     36   16186 use Mouse::Util::TypeConstraints;
  36         78  
  36         318  
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 343 my ($class, $type) = @_;
22              
23 58         80 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         153 my ($elemental_type, @other_types) = split(/\|/, $type);
31              
32 58 100       239 if ($type =~ m{^Elive::}) {
33              
34 39 100       383 if ($type->can('element_class')) {
35 35   100     157 ($elemental_type, @other_types) = split(/\|/, $type->element_class || 'Str');
36 35         440 $array_type = $type;
37             }
38             }
39              
40 58         148 $info{type} = $type;
41 58 100       195 $info{array_type} = $array_type if defined $array_type;
42 58         131 $info{elemental_type} = $elemental_type;
43 58         118 $info{_other_types} = \@other_types;
44              
45 58         679 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 42 my $self = shift;
69              
70 27         69 my $elemental_type = $self->elemental_type;
71 27   66     329 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 7 my $self = shift;
83              
84 4   66     8 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 1648 my $self = shift;
102              
103 8         46 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;