1 package MooseX::Meta::TypeConstraint::Structured::Optional;
4 use Moose::Meta::TypeConstraint ();
6 #extends 'Moose::Meta::TypeConstraint';
7 extends 'MooseX::Meta::TypeConstraint::Structured::Positional';
8 with 'MooseX::Meta::TypeConstraint::Role::Structured';
12 MooseX::Meta::TypeConstraint::Structured::Optional - Structured Type Constraints
16 The follow is example usage:
18 use Moose::Util::TypeConstraints;
19 use MooseX::Meta::TypeConstraint::Structured::Optional;
21 my @options = ('Str', 'Int');
23 my $tc = MooseX::Meta::TypeConstraint::Structured::Optional->new(
25 parent => find_type_constraint('ArrayRef'),
27 find_type_constraint($_);
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:
38 MyType[Int,Str,Optional[Int, Int]]
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:
45 ->validate(1,'hello',2,3);
46 ->validate(1,'hello',2);
47 ->validate(1,'hello');
51 ->validate(1,'hello',[2,3]]);
52 ->validate(1,'hello',[2]]);
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.
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.
64 This class defines the following attributes.
66 =head2 containing_type_constraint ($structured_type_constraint)
68 This is the type constraint that contains the Optional parameters.
72 #has 'containing_type_constraint' => (
74 # does=>'MooseX::Meta::TypeConstraint::Role::Structured',
80 This is a signature of internal contraints for the contents of the outer
85 has '+signature' => (isa=>'ArrayRef[Moose::Meta::TypeConstraint]');
89 This class defines the following methods.
91 =head2 _normalize_args
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>).
99 #sub _normalize_args {
100 # return shift->containing_type_constraint->_normalize_args(@_);
105 The constraint is basically validating the L</signature> against the incoming
111 # return shift->containing_type_constraint->constraint(@_);
114 =head2 _parse_type_parameter ($str)
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.
122 #sub _parse_type_parameter {
123 # return shift->containing_type_constraint->_parse_type_parameter(@_);
127 =head2 signature_equals
129 Check that the signature equals another signature. Delegated to the containing
134 #sub signature_equals {
135 # return shift->containing_type_constraint->signature_equals(@_);
140 John James Napiorkowski <jjnapiork@cpan.org>
144 You may distribute this code under the same terms as Perl itself.