Commit | Line | Data |
4c2125a4 |
1 | package MooseX::Types::TypeDecorator; |
2 | |
a706b0f2 |
3 | use strict; |
4 | use warnings; |
4c2125a4 |
5 | |
bb5b7b28 |
6 | use Carp::Clan qw( ^MooseX::Types ); |
475bbd1d |
7 | use Moose::Util::TypeConstraints (); |
bb5b7b28 |
8 | use Moose::Meta::TypeConstraint::Union; |
371efa05 |
9 | use Scalar::Util qw(blessed); |
bb5b7b28 |
10 | |
4c2125a4 |
11 | use overload( |
12 | '""' => sub { |
686e5888 |
13 | return shift->__type_constraint->name; |
4c2125a4 |
14 | }, |
cf1a8bfa |
15 | '|' => sub { |
686e5888 |
16 | |
17 | ## It's kind of ugly that we need to know about Union Types, but this |
18 | ## is needed for syntax compatibility. Maybe someday we'll all just do |
19 | ## Or[Str,Str,Int] |
20 | |
371efa05 |
21 | my @tc = grep {blessed $_} @_; |
bb5b7b28 |
22 | my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc); |
23 | return Moose::Util::TypeConstraints::register_type_constraint($union); |
cf1a8bfa |
24 | }, |
1d9a68a6 |
25 | fallback => 1, |
26 | |
4c2125a4 |
27 | ); |
28 | |
371efa05 |
29 | |
4c2125a4 |
30 | =head1 NAME |
31 | |
32 | MooseX::Types::TypeDecorator - More flexible access to a Type Constraint |
33 | |
34 | =head1 DESCRIPTION |
35 | |
36 | This is a decorator object that contains an underlying type constraint. We use |
37 | this to control access to the type constraint and to add some features. |
38 | |
a706b0f2 |
39 | =head1 METHODS |
4c2125a4 |
40 | |
a706b0f2 |
41 | This class defines the following methods. |
4c2125a4 |
42 | |
a706b0f2 |
43 | =head2 new |
4c2125a4 |
44 | |
a706b0f2 |
45 | Old school instantiation |
4c2125a4 |
46 | |
47 | =cut |
48 | |
a706b0f2 |
49 | sub new { |
475bbd1d |
50 | my $class = shift @_; |
51 | if(my $arg = shift @_) { |
371efa05 |
52 | if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) { |
475bbd1d |
53 | return bless {'__type_constraint'=>$arg}, $class; |
e7d06577 |
54 | } elsif( |
55 | blessed $arg && |
56 | $arg->isa('MooseX::Types::UndefinedType') |
57 | ) { |
475bbd1d |
58 | ## stub in case we'll need to handle these types differently |
59 | return bless {'__type_constraint'=>$arg}, $class; |
371efa05 |
60 | } elsif(blessed $arg) { |
61 | croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg; |
475bbd1d |
62 | } else { |
d8f30dd4 |
63 | croak "Argument cannot be '$arg'"; |
475bbd1d |
64 | } |
bb5b7b28 |
65 | } else { |
e7d06577 |
66 | croak "This method [new] requires a single argument."; |
bb5b7b28 |
67 | } |
a706b0f2 |
68 | } |
4c2125a4 |
69 | |
686e5888 |
70 | =head __type_constraint ($type_constraint) |
4c2125a4 |
71 | |
e088dd03 |
72 | Set/Get the type_constraint. |
4c2125a4 |
73 | |
74 | =cut |
75 | |
475bbd1d |
76 | sub __type_constraint { |
a706b0f2 |
77 | my $self = shift @_; |
371efa05 |
78 | |
79 | if(blessed $self) { |
80 | if(defined(my $tc = shift @_)) { |
81 | $self->{__type_constraint} = $tc; |
82 | } |
83 | return $self->{__type_constraint}; |
84 | } else { |
85 | croak 'cannot call __type_constraint as a class method'; |
a706b0f2 |
86 | } |
a706b0f2 |
87 | } |
4c2125a4 |
88 | |
bb5b7b28 |
89 | =head2 isa |
90 | |
91 | handle $self->isa since AUTOLOAD can't. |
92 | |
93 | =cut |
94 | |
95 | sub isa { |
371efa05 |
96 | my ($self, $target) = @_; |
bb5b7b28 |
97 | if(defined $target) { |
475bbd1d |
98 | return $self->__type_constraint->isa($target); |
bb5b7b28 |
99 | } else { |
100 | return; |
101 | } |
102 | } |
103 | |
104 | =head2 can |
105 | |
106 | handle $self->can since AUTOLOAD can't. |
107 | |
108 | =cut |
109 | |
110 | sub can { |
111 | my ($self, $target) = @_; |
112 | if(defined $target) { |
475bbd1d |
113 | return $self->__type_constraint->can($target); |
bb5b7b28 |
114 | } else { |
115 | return; |
116 | } |
117 | } |
118 | |
a706b0f2 |
119 | =head2 DESTROY |
4c2125a4 |
120 | |
a706b0f2 |
121 | We might need it later |
4c2125a4 |
122 | |
a706b0f2 |
123 | =cut |
4c2125a4 |
124 | |
a706b0f2 |
125 | sub DESTROY { |
126 | return; |
127 | } |
4c2125a4 |
128 | |
a706b0f2 |
129 | =head2 AUTOLOAD |
4c2125a4 |
130 | |
a706b0f2 |
131 | Delegate to the decorator targe |
4c2125a4 |
132 | |
a706b0f2 |
133 | =cut |
4c2125a4 |
134 | |
e088dd03 |
135 | sub AUTOLOAD { |
077ac262 |
136 | |
475bbd1d |
137 | my ($self, @args) = @_; |
a706b0f2 |
138 | my ($method) = (our $AUTOLOAD =~ /([^:]+)$/); |
077ac262 |
139 | |
140 | ## We delegate with this method in an attempt to support a value of |
141 | ## __type_constraint which is also AUTOLOADing, in particular the class |
142 | ## MooseX::Types::UndefinedType which AUTOLOADs during autovivication. |
143 | |
144 | my $return; |
145 | |
146 | eval { |
147 | $return = $self->__type_constraint->$method(@args); |
148 | }; if($@) { |
149 | croak $@; |
475bbd1d |
150 | } else { |
077ac262 |
151 | return $return; |
475bbd1d |
152 | } |
a706b0f2 |
153 | } |
4c2125a4 |
154 | |
155 | =head1 AUTHOR AND COPYRIGHT |
156 | |
157 | John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org> |
158 | |
159 | =head1 LICENSE |
160 | |
161 | This program is free software; you can redistribute it and/or modify |
162 | it under the same terms as perl itself. |
163 | |
164 | =cut |
165 | |
166 | 1; |