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