Commit | Line | Data |
---|---|---|
4c2125a4 | 1 | package MooseX::Types::TypeDecorator; |
2 | ||
a706b0f2 | 3 | use strict; |
4 | use warnings; | |
4c2125a4 | 5 | |
cf1a8bfa | 6 | use Moose::Util::TypeConstraints; |
4c2125a4 | 7 | use overload( |
8 | '""' => sub { | |
9 | shift->type_constraint->name; | |
10 | }, | |
cf1a8bfa | 11 | '|' => sub { |
12 | my @names = grep {$_} map {"$_"} @_; | |
13 | ## Don't know why I can't use the array version of this... | |
14 | my $names = join('|', @names); | |
15 | Moose::Util::TypeConstraints::create_type_constraint_union($names); | |
16 | }, | |
4c2125a4 | 17 | ); |
18 | ||
19 | =head1 NAME | |
20 | ||
21 | MooseX::Types::TypeDecorator - More flexible access to a Type Constraint | |
22 | ||
23 | =head1 DESCRIPTION | |
24 | ||
25 | This is a decorator object that contains an underlying type constraint. We use | |
26 | this to control access to the type constraint and to add some features. | |
27 | ||
a706b0f2 | 28 | =head1 METHODS |
4c2125a4 | 29 | |
a706b0f2 | 30 | This class defines the following methods. |
4c2125a4 | 31 | |
a706b0f2 | 32 | =head2 new |
4c2125a4 | 33 | |
a706b0f2 | 34 | Old school instantiation |
4c2125a4 | 35 | |
36 | =cut | |
37 | ||
a706b0f2 | 38 | sub new { |
39 | my ($class, %args) = @_; | |
40 | return bless \%args, $class; | |
41 | } | |
4c2125a4 | 42 | |
a706b0f2 | 43 | =head type_constraint ($type_constraint) |
4c2125a4 | 44 | |
a706b0f2 | 45 | Set/Get the type_constraint |
4c2125a4 | 46 | |
47 | =cut | |
48 | ||
a706b0f2 | 49 | sub type_constraint { |
50 | my $self = shift @_; | |
51 | if(my $tc = shift @_) { | |
52 | $self->{type_constraint} = $tc; | |
53 | } | |
54 | return $self->{type_constraint}; | |
55 | } | |
4c2125a4 | 56 | |
a706b0f2 | 57 | =head2 DESTROY |
4c2125a4 | 58 | |
a706b0f2 | 59 | We might need it later |
4c2125a4 | 60 | |
a706b0f2 | 61 | =cut |
4c2125a4 | 62 | |
a706b0f2 | 63 | sub DESTROY { |
64 | return; | |
65 | } | |
4c2125a4 | 66 | |
a706b0f2 | 67 | =head2 AUTOLOAD |
4c2125a4 | 68 | |
a706b0f2 | 69 | Delegate to the decorator targe |
4c2125a4 | 70 | |
a706b0f2 | 71 | =cut |
4c2125a4 | 72 | |
a706b0f2 | 73 | sub AUTOLOAD |
74 | { | |
75 | my ($method) = (our $AUTOLOAD =~ /([^:]+)$/); | |
76 | return shift->type_constraint->$method(@_); | |
77 | } | |
4c2125a4 | 78 | |
79 | =head1 AUTHOR AND COPYRIGHT | |
80 | ||
81 | John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org> | |
82 | ||
83 | =head1 LICENSE | |
84 | ||
85 | This program is free software; you can redistribute it and/or modify | |
86 | it under the same terms as perl itself. | |
87 | ||
88 | =cut | |
89 | ||
90 | 1; |