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