1 package MooseX::Meta::TypeConstraint::Structured::Optional;
4 use Moose::Meta::TypeConstraint ();
6 extends 'Moose::Meta::TypeConstraint';
7 with 'MooseX::Meta::TypeConstraint::Role::Structured';
11 MooseX::Meta::TypeConstraint::Structured::Optional - Structured Type Constraints
15 The follow is example usage:
17 use Moose::Util::TypeConstraints;
18 use MooseX::Meta::TypeConstraint::Structured::Optional;
20 my @options = ('Str', 'Int');
22 my $tc = MooseX::Meta::TypeConstraint::Structured::Optional->new(
24 parent => find_type_constraint('ArrayRef'),
26 find_type_constraint($_);
32 Optional Type Constraints are additional constraints on a 'base' structured
33 type constraint which extends those constraints with additional optional
34 fields. Basically this constraint get's it's constraint logic and args
35 from a a Structured Type Constraint that contains it. So basically:
37 MyType[Int,Str,Optional[Int, Int]]
39 In this example, the structured Type constraint 'MyType' is the container for
40 this Optional type called 'Optional'. What will happen here is that the
41 MyType will get the first elements for validation and a third one will go
42 to optional. Optional will 'inline' itself so that you can validate with:
44 ->validate(1,'hello',2,3);
45 ->validate(1,'hello',2);
46 ->validate(1,'hello');
50 ->validate(1,'hello',[2,3]]);
51 ->validate(1,'hello',[2]]);
53 as you might expect. Basically it sucks up args to the length of it's declared
56 Please keep in mind the type constraint names given in this example are for
57 example use only and any similarity between them, actual Type Constraints and
58 package names are coincidental.
62 This class defines the following attributes.
64 =head2 containing_type_constraint ($structured_type_constraint)
66 This is the type constraint that contains the Optional parameters.
70 has 'containing_type_constraint' => (
72 does=>'MooseX::Meta::TypeConstraint::Role::Structured',
78 This is a signature of internal contraints for the contents of the outer
83 has '+signature' => (isa=>'ArrayRef[Moose::Meta::TypeConstraint]');
87 This class defines the following methods.
89 =head2 _normalize_args
91 Get arguments into a known state or die trying. Ideally we try to make this
92 into a HashRef so we can match it up with the L</signature> HashRef. This gets
93 delegated to the containing class (L</containing_type_constraint>).
98 return shift->containing_type_constraint->_normalize_args(@_);
103 The constraint is basically validating the L</signature> against the incoming
108 return shift->containing_type_constraint->constraint(@_);
111 =head2 parse_parameter_str ($str)
113 Given a $string that is the parameter information part of a parameterized
114 constraint, parses it for internal constraint information. This is delegated
115 to the containing class.
119 sub parse_parameter_str {
120 return shift->containing_type_constraint->parse_parameter_str(@_);
124 =head2 signature_equals
126 Check that the signature equals another signature. Delegated to the containing
131 sub signature_equals {
132 return shift->containing_type_constraint->signature_equals(@_);
137 John James Napiorkowski <jjnapiork@cpan.org>
141 You may distribute this code under the same terms as Perl itself.