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