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 | }, |
4c2125a4 |
25 | ); |
26 | |
371efa05 |
27 | |
4c2125a4 |
28 | =head1 NAME |
29 | |
30 | MooseX::Types::TypeDecorator - More flexible access to a Type Constraint |
31 | |
32 | =head1 DESCRIPTION |
33 | |
34 | This is a decorator object that contains an underlying type constraint. We use |
35 | this to control access to the type constraint and to add some features. |
36 | |
a706b0f2 |
37 | =head1 METHODS |
4c2125a4 |
38 | |
a706b0f2 |
39 | This class defines the following methods. |
4c2125a4 |
40 | |
a706b0f2 |
41 | =head2 new |
4c2125a4 |
42 | |
a706b0f2 |
43 | Old school instantiation |
4c2125a4 |
44 | |
45 | =cut |
46 | |
a706b0f2 |
47 | sub new { |
475bbd1d |
48 | my $class = shift @_; |
49 | if(my $arg = shift @_) { |
371efa05 |
50 | if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) { |
475bbd1d |
51 | return bless {'__type_constraint'=>$arg}, $class; |
371efa05 |
52 | } elsif(blessed $arg && $arg->isa('MooseX::Types::UndefinedType')) { |
475bbd1d |
53 | ## stub in case we'll need to handle these types differently |
54 | return bless {'__type_constraint'=>$arg}, $class; |
371efa05 |
55 | } elsif(blessed $arg) { |
56 | croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg; |
475bbd1d |
57 | } else { |
d8f30dd4 |
58 | croak "Argument cannot be '$arg'"; |
475bbd1d |
59 | } |
bb5b7b28 |
60 | } else { |
686e5888 |
61 | croak "This method [new] requires a single argument of 'arg'."; |
bb5b7b28 |
62 | } |
a706b0f2 |
63 | } |
4c2125a4 |
64 | |
686e5888 |
65 | =head __type_constraint ($type_constraint) |
4c2125a4 |
66 | |
e088dd03 |
67 | Set/Get the type_constraint. |
4c2125a4 |
68 | |
69 | =cut |
70 | |
475bbd1d |
71 | sub __type_constraint { |
a706b0f2 |
72 | my $self = shift @_; |
371efa05 |
73 | |
74 | if(blessed $self) { |
75 | if(defined(my $tc = shift @_)) { |
76 | $self->{__type_constraint} = $tc; |
77 | } |
78 | return $self->{__type_constraint}; |
79 | } else { |
80 | croak 'cannot call __type_constraint as a class method'; |
a706b0f2 |
81 | } |
a706b0f2 |
82 | } |
4c2125a4 |
83 | |
bb5b7b28 |
84 | =head2 isa |
85 | |
86 | handle $self->isa since AUTOLOAD can't. |
87 | |
88 | =cut |
89 | |
90 | sub isa { |
371efa05 |
91 | my ($self, $target) = @_; |
bb5b7b28 |
92 | if(defined $target) { |
475bbd1d |
93 | return $self->__type_constraint->isa($target); |
bb5b7b28 |
94 | } else { |
95 | return; |
96 | } |
97 | } |
98 | |
99 | =head2 can |
100 | |
101 | handle $self->can since AUTOLOAD can't. |
102 | |
103 | =cut |
104 | |
105 | sub can { |
106 | my ($self, $target) = @_; |
107 | if(defined $target) { |
475bbd1d |
108 | return $self->__type_constraint->can($target); |
bb5b7b28 |
109 | } else { |
110 | return; |
111 | } |
112 | } |
113 | |
a706b0f2 |
114 | =head2 DESTROY |
4c2125a4 |
115 | |
a706b0f2 |
116 | We might need it later |
4c2125a4 |
117 | |
a706b0f2 |
118 | =cut |
4c2125a4 |
119 | |
a706b0f2 |
120 | sub DESTROY { |
121 | return; |
122 | } |
4c2125a4 |
123 | |
a706b0f2 |
124 | =head2 AUTOLOAD |
4c2125a4 |
125 | |
a706b0f2 |
126 | Delegate to the decorator targe |
4c2125a4 |
127 | |
a706b0f2 |
128 | =cut |
4c2125a4 |
129 | |
e088dd03 |
130 | sub AUTOLOAD { |
475bbd1d |
131 | my ($self, @args) = @_; |
a706b0f2 |
132 | my ($method) = (our $AUTOLOAD =~ /([^:]+)$/); |
475bbd1d |
133 | if($self->__type_constraint->can($method)) { |
134 | return $self->__type_constraint->$method(@args); |
135 | } else { |
136 | croak "Method '$method' is not supported"; |
137 | } |
a706b0f2 |
138 | } |
4c2125a4 |
139 | |
140 | =head1 AUTHOR AND COPYRIGHT |
141 | |
142 | John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org> |
143 | |
144 | =head1 LICENSE |
145 | |
146 | This program is free software; you can redistribute it and/or modify |
147 | it under the same terms as perl itself. |
148 | |
149 | =cut |
150 | |
151 | 1; |