69ac55e34077dc0f045a314998e142c9422062ab
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / OptionTypeMap.pm
1
2 package MooseX::Getopt::OptionTypeMap;
3
4 use Moose 'confess';
5 use Moose::Util::TypeConstraints 'find_type_constraint';
6
7 our $VERSION   = '0.01';
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 my %option_type_map = (
11     'Bool'     => '!',
12     'Str'      => '=s',
13     'Int'      => '=i',
14     'Float'    => '=f',
15     'ArrayRef' => '=s@',
16     'HashRef'  => '=s%',    
17 );
18
19 sub has_option_type {
20     my (undef, $type_name) = @_;
21     return 1 if exists $option_type_map{$type_name};
22
23     my $current = find_type_constraint($type_name);
24     while (my $parent = $current->parent) {
25         return 1 if exists $option_type_map{$parent->name};
26         $current = $parent;
27     }
28
29     return 0;
30 }
31
32 sub get_option_type {
33     my (undef, $type_name) = @_;
34     return $option_type_map{$type_name}
35         if exists $option_type_map{$type_name};
36
37     my $current = find_type_constraint($type_name);
38     while (my $parent = $current->parent) {
39         return $option_type_map{$parent->name}
40             if exists $option_type_map{$parent->name};
41         $current = $parent;
42     }
43
44     return;
45 }
46
47 sub add_option_type_to_map {
48     my (undef, $type_name, $option_string) = @_;
49     (defined $type_name && defined $option_string)
50         || confess "You must supply both a type name and an option string";
51     (find_type_constraint($type_name))
52         || confess "The type constraint '$type_name' does not exist";
53     $option_type_map{$type_name} = $option_string;
54 }
55
56 no Moose; no Moose::Util::TypeConstraints; 1;
57
58 __END__
59
60
61 =pod
62
63 =head1 NAME
64
65 MooseX::Getopt::OptionTypeMap - Storage for the option to type mappings
66
67 =head1 DESCRIPTION
68
69 See the I<Custom Type Constraints> section in the L<MooseX::Getopt> docs
70 for more info about how to use this module.
71
72 =head1 METHODS
73
74 These are all class methods and should be called as such.
75
76 =over 4
77
78 =item B<has_option_type ($type_name)>
79
80 =item B<get_option_type ($type_name)>
81
82 =item B<add_option_type_to_map ($type_name, $option_spec)>
83
84 =back
85
86 =head1 BUGS
87
88 All complex software has bugs lurking in it, and this module is no 
89 exception. If you find a bug please either email me, or add the bug
90 to cpan-RT.
91
92 =head1 AUTHOR
93
94 Stevan Little E<lt>stevan@iinteractive.comE<gt>
95
96 =head1 COPYRIGHT AND LICENSE
97
98 Copyright 2007 by Infinity Interactive, Inc.
99
100 L<http://www.iinteractive.com>
101
102 This library is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself.
104
105 =cut