6ded368f7cb2fc776ba00972754b9d1eebcbed3a
[gitmo/MooseX-Types.git] / t / lib / DecoratorLibrary.pm
1 package DecoratorLibrary;
2
3 use warnings;
4 use strict;
5
6 use MooseX::Types::Moose qw( Str ArrayRef HashRef Int );
7 use MooseX::Types
8     -declare => [qw(
9         MyArrayRefBase
10         MyArrayRefInt01
11         MyArrayRefInt02
12         MyHashRefOfInts
13         MyHashRefOfStr
14         StrOrArrayRef
15         AtLeastOneInt
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 coerce MyArrayRefInt01,
29     from Str,
30     via {[split('\.',$_)]},
31     from HashRef,
32     via {[sort values(%$_)]};
33     
34 subtype MyArrayRefInt02,
35     as MyArrayRefBase[Int];
36     
37 subtype MyHashRefOfInts,
38     as HashRef[Int];
39     
40 subtype MyHashRefOfStr,
41     as HashRef[Str];
42
43 coerce MyArrayRefInt02,
44     from Str,
45     via {[split(':',$_)]},
46     from MyHashRefOfInts,
47     via {[sort values(%$_)]},
48     from MyHashRefOfStr,
49     via {[ sort map { length $_ } values(%$_) ]},
50     ## Can't do HashRef[ArrayRef] here since if I do HashRef get the via {}
51     ## Stuff passed as args.  
52     from HashRef([ArrayRef]),
53     via {[ sort map { @$_ } values(%$_) ]};
54
55 subtype StrOrArrayRef,
56     as Str|ArrayRef;
57     
58 subtype AtLeastOneInt,
59     ## Same problem as MyArrayRefInt02, see above.  Another way to solve it by
60     ## forcing some sort of context.  Tried to fix this with method prototypes
61     ## but just couldn't make it work.
62     as (ArrayRef[Int]),
63     where { @$_ > 0 };
64 1;