added a tags directory for releases
[gitmo/MooseX-Types.git] / t / lib / DecoratorLibrary.pm
1 package DecoratorLibrary;
2
3 use MooseX::Types::Moose qw( Str ArrayRef HashRef Int );
4 use MooseX::Types
5     -declare => [qw(
6         MyArrayRefBase
7         MyArrayRefInt01
8         MyArrayRefInt02
9         MyHashRefOfInts
10         MyHashRefOfStr
11         StrOrArrayRef
12         AtLeastOneInt
13         Jobs
14         SubOfMyArrayRefInt01
15         BiggerInt
16     )];
17
18 subtype MyArrayRefBase,
19     as ArrayRef;
20     
21 coerce MyArrayRefBase,
22     from Str,
23     via {[split(',', $_)]};
24     
25 subtype MyArrayRefInt01,
26     as ArrayRef[Int];
27
28 subtype BiggerInt,
29     as Int,
30     where {$_>10};
31
32 ## We can change this when the .61 Moose comes out.  When that happens we will
33 ## have the correct patch to Moose::Meta::TypeConstraint::Parameterized to let
34 ## us support parameterizing parameterized subtypes.  When we get this we can
35 ## then replace the where clause with:
36
37     ##as MyArrayRefInt01[BiggerInt];
38
39 subtype SubOfMyArrayRefInt01,
40     as MyArrayRefInt01,
41     where {
42         my $ok_or_not = 1;
43         foreach my $int (@$_) {
44             $ok_or_not = $int>10 ? 1:0
45              if $ok_or_not;
46         } $ok_or_not;
47     };
48
49 coerce MyArrayRefInt01,
50     from Str,
51     via {[split('\.',$_)]},
52     from HashRef,
53     via {[sort values(%$_)]};
54     
55 subtype MyArrayRefInt02,
56     as MyArrayRefBase[Int];
57     
58 subtype MyHashRefOfInts,
59     as HashRef[Int];
60     
61 subtype MyHashRefOfStr,
62     as HashRef[Str];
63
64 coerce MyArrayRefInt02,
65     from Str,
66     via {[split(':',$_)]},
67     from MyHashRefOfInts,
68     via {[sort values(%$_)]},
69     from MyHashRefOfStr,
70     via {[ sort map { length $_ } values(%$_) ]},
71     from HashRef[ArrayRef],
72     via {[ sort map { @$_ } values(%$_) ]};
73
74 subtype StrOrArrayRef,
75     as Str|ArrayRef;
76
77 subtype AtLeastOneInt,
78     as ArrayRef[Int],
79     where { @$_ > 0 };
80     
81 enum Jobs,
82     (qw/Programming Teaching Banking/);
83     
84 1;