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