got the basic function for Optional, but the regex is still troubled, now is having...
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured / Optional.pm
CommitLineData
7e2f0558 1package MooseX::Meta::TypeConstraint::Structured::Optional;
2
3use Moose;
4use Moose::Meta::TypeConstraint ();
5
99b27cbd 6#extends 'Moose::Meta::TypeConstraint';
7extends 'MooseX::Meta::TypeConstraint::Structured::Positional';
7e2f0558 8with 'MooseX::Meta::TypeConstraint::Role::Structured';
9
10=head1 NAME
11
12MooseX::Meta::TypeConstraint::Structured::Optional - Structured Type Constraints
13
14=head1 SYNOPSIS
15
16The follow is example usage:
17
18 use Moose::Util::TypeConstraints;
19 use MooseX::Meta::TypeConstraint::Structured::Optional;
20
21 my @options = ('Str', 'Int');
22
23 my $tc = MooseX::Meta::TypeConstraint::Structured::Optional->new(
24 name => 'Dict',
25 parent => find_type_constraint('ArrayRef'),
26 signature => [map {
27 find_type_constraint($_);
28 } @options],
29 );
30
31=head1 DESCRIPTION
32
33Optional Type Constraints are additional constraints on a 'base' structured
34type constraint which extends those constraints with additional optional
35fields. Basically this constraint get's it's constraint logic and args
36from a a Structured Type Constraint that contains it. So basically:
37
38 MyType[Int,Str,Optional[Int, Int]]
39
40In this example, the structured Type constraint 'MyType' is the container for
41this Optional type called 'Optional'. What will happen here is that the
42MyType will get the first elements for validation and a third one will go
43to optional. Optional will 'inline' itself so that you can validate with:
44
45 ->validate(1,'hello',2,3);
46 ->validate(1,'hello',2);
47 ->validate(1,'hello');
48
49and not:
50
51 ->validate(1,'hello',[2,3]]);
52 ->validate(1,'hello',[2]]);
53
54as you might expect. Basically it sucks up args to the length of it's declared
99b27cbd 55type constraints. So Optional args are validated against the definition, but if
56they are missing this does not cause a validation error.
7e2f0558 57
58Please keep in mind the type constraint names given in this example are for
59example use only and any similarity between them, actual Type Constraints and
60package names are coincidental.
61
62=head1 ATTRIBUTES
63
64This class defines the following attributes.
65
66=head2 containing_type_constraint ($structured_type_constraint)
67
68This is the type constraint that contains the Optional parameters.
69
70=cut
71
99b27cbd 72#has 'containing_type_constraint' => (
73# is=>'ro',
74# does=>'MooseX::Meta::TypeConstraint::Role::Structured',
75# required=>1,
76#);
7e2f0558 77
78=head2 signature
79
80This is a signature of internal contraints for the contents of the outer
81contraint container.
82
83=cut
84
85has '+signature' => (isa=>'ArrayRef[Moose::Meta::TypeConstraint]');
86
87=head1 METHODS
88
89This class defines the following methods.
90
91=head2 _normalize_args
92
93Get arguments into a known state or die trying. Ideally we try to make this
94into a HashRef so we can match it up with the L</signature> HashRef. This gets
95delegated to the containing class (L</containing_type_constraint>).
96
97=cut
98
99b27cbd 99#sub _normalize_args {
100# return shift->containing_type_constraint->_normalize_args(@_);
101#}
7e2f0558 102
103=head2 constraint
104
105The constraint is basically validating the L</signature> against the incoming
106
107=cut
108
99b27cbd 109#sub constraint {
110# return 1;
111 # return shift->containing_type_constraint->constraint(@_);
112#}
7e2f0558 113
99b27cbd 114=head2 _parse_type_parameter ($str)
7e2f0558 115
116Given a $string that is the parameter information part of a parameterized
117constraint, parses it for internal constraint information. This is delegated
118to the containing class.
119
120=cut
121
99b27cbd 122#sub _parse_type_parameter {
123# return shift->containing_type_constraint->_parse_type_parameter(@_);
124#}
7e2f0558 125
126
127=head2 signature_equals
128
129Check that the signature equals another signature. Delegated to the containing
130class.
131
132=cut
133
99b27cbd 134#sub signature_equals {
135# return shift->containing_type_constraint->signature_equals(@_);
136#}
7e2f0558 137
138=head1 AUTHOR
139
140John James Napiorkowski <jjnapiork@cpan.org>
141
142=head1 LICENSE
143
144You may distribute this code under the same terms as Perl itself.
145
146=cut
147
148no Moose; 1;