create a basic type, clarified and regularized some of the naming conventions for...
[gitmo/MooseX-Dependent.git] / lib / MooseX / Types / Dependent.pm
CommitLineData
a018b5bb 1package MooseX::Types::Dependent;
2
3cfd35fd 3use 5.008;
4
5use Moose::Util::TypeConstraints;
6use MooseX::Meta::TypeConstraint::Dependent;
7use MooseX::Types -declare => [qw(Depending)];
8
9our $VERSION = '0.01';
10our $AUTHORITY = 'cpan:JJNAPIORK';
a018b5bb 11
12=head1 NAME
13
3cfd35fd 14MooseX::Types::Dependent - L<MooseX::Types> constraints that depend on values.
a018b5bb 15
16=head1 SYNOPSIS
17
9b6d2e22 18 subtype UniqueInt,
19 as Depending[
20 Int,
21 sub {
22 shift->exists(shift) ? 0:1;
23 },
24 Set,
25 ];
26
27 subtype UniqueInt,
28 as Depending {
29 shift->exists(shift) ? 0:1;
30 } [Int, Set];
3cfd35fd 31
32Please see the test cases for more examples.
33
34=head1 DEFINITIONS
35
36The following is a list of terms used in this documentation.
37
38=head2 Dependent Type Constraint
39
40=head2 Check Value
41
42=head2 Constraining Type Constraint
43
44=head2 Constraining Value
a018b5bb 45
46=head1 DESCRIPTION
47
3cfd35fd 48A dependent type is a type constraint whose validity is dependent on a second
49value. You defined the dependent type constraint with a primary type constraint
50(such as 'Int') a 'constraining' value type constraint (such as a Set object)
51and a coderef which will compare the incoming value to be checked with a value
52that conforms to the constraining type constraint. Typically there should be a
53comparision operator between the check value and the constraining value
a018b5bb 54
3cfd35fd 55=head2 Subtyping a Dependent type constraints
a018b5bb 56
9b6d2e22 57TDB: Need discussion and examples.
a018b5bb 58
3cfd35fd 59=head2 Coercions
a018b5bb 60
9b6d2e22 61TBD: Need discussion and example of coercions working for both the
62constrainted and dependent type constraint.
a018b5bb 63
3cfd35fd 64=head2 Recursion
a018b5bb 65
3cfd35fd 66Newer versions of L<MooseX::Types> support recursive type constraints. That is
67you can include a type constraint as a contained type constraint of itself.
68Recursion is support in both the dependent and constraining type constraint. For
69example:
a018b5bb 70
3cfd35fd 71=head1 TYPE CONSTRAINTS
a018b5bb 72
3cfd35fd 73This type library defines the following constraints.
a018b5bb 74
3cfd35fd 75=head2 Depending[$dependent_tc, $codref, $constraining_tc]
a018b5bb 76
3cfd35fd 77Create a subtype of $dependent_tc that is constrainted by a value that is a
9b6d2e22 78valid $constraining_tc using $coderef. For example:
a018b5bb 79
3cfd35fd 80 subtype GreaterThanInt,
81 as Depending[
82 Int,
83 sub {
84 my($constraining_value, $check_value) = @_;
85 return $constraining_value > $check_value ? 1:0;
86 },
87 Int,
88 ];
a018b5bb 89
9b6d2e22 90Note that the coderef is passed the constraining value and the check value as an
91Array NOT an ArrayRef.
92
3cfd35fd 93This would create a type constraint that takes an integer and checks it against
94a second integer, requiring that the check value is greater. For example:
a018b5bb 95
9b6d2e22 96 GreaterThanInt->check([5,10]); ## Fails, 5 is less than 10
97 GreaterThanInt->check(['a',10]); ## Fails, 'a' is not an Int.
98 GreaterThanInt->check([5,'b']); ## Fails, 'b' is not an Int either.
99 GreaterThanInt->check([10,5]); ## Success, 10 is greater than 5.
a018b5bb 100
3cfd35fd 101=head1 EXAMPLES
a018b5bb 102
3cfd35fd 103Here are some additional example usage for structured types. All examples can
104be found also in the 't/examples.t' test. Your contributions are also welcomed.
a018b5bb 105
9b6d2e22 106TBD
a018b5bb 107
108=cut
109
3cfd35fd 110Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint(
9b6d2e22 111 MooseX::Meta::TypeConstraint::Dependent->new(
112 name => "MooseX::Types::Dependent::Depending" ,
113 parent => find_type_constraint('ArrayRef'),
114 constraint_generator=> sub {
115 my ($dependent_val, $callback, $constraining_val) = @_;
116 return $callback->($dependent_val, $constraining_val);
117 },
118 )
3cfd35fd 119);
9b6d2e22 120
3cfd35fd 121=head1 SEE ALSO
a018b5bb 122
3cfd35fd 123The following modules or resources may be of interest.
a018b5bb 124
3cfd35fd 125L<Moose>, L<MooseX::Types>, L<Moose::Meta::TypeConstraint>,
126L<MooseX::Meta::TypeConstraint::Dependent>
a018b5bb 127
3cfd35fd 128=head1 TODO
a018b5bb 129
3cfd35fd 130Here's a list of stuff I would be happy to get volunteers helping with:
a018b5bb 131
3cfd35fd 132=over 4
a018b5bb 133
3cfd35fd 134=item Examples
a018b5bb 135
3cfd35fd 136Examples of useful code with both POD and ideally a test case to show it
137working.
a018b5bb 138
3cfd35fd 139=back
140
141=head1 AUTHOR
a018b5bb 142
3cfd35fd 143John Napiorkowski, C<< <jjnapiork@cpan.org> >>
144
145=head1 COPYRIGHT & LICENSE
a018b5bb 146
147This program is free software; you can redistribute it and/or modify
3cfd35fd 148it under the same terms as Perl itself.
a018b5bb 149
150=cut
9b6d2e22 151
a018b5bb 1521;