Commit | Line | Data |
8034a232 |
1 | |
2 | package MooseX::Getopt::OptionTypeMap; |
3 | |
365e5784 |
4 | use Moose 'confess', 'blessed'; |
8034a232 |
5 | use Moose::Util::TypeConstraints 'find_type_constraint'; |
6 | |
e4ab19b9 |
7 | our $VERSION = '0.04'; |
8034a232 |
8 | our $AUTHORITY = 'cpan:STEVAN'; |
9 | |
c5c99e6b |
10 | |
8034a232 |
11 | my %option_type_map = ( |
c5c99e6b |
12 | 'Bool' => '!', |
13 | 'Str' => '=s', |
14 | 'Int' => '=i', |
15 | 'Num' => '=f', |
16 | 'ArrayRef' => '=s@', |
17 | 'HashRef' => '=s%', |
18 | 'Bool|Str' => ':s', |
19 | 'Bool|Int' => ':i', |
20 | 'Bool|Num' => ':f', |
21 | 'Str|Bool' => ':s', |
22 | 'Int|Bool' => ':i', |
23 | 'Num|Bool' => ':f', |
8034a232 |
24 | ); |
25 | |
c5c99e6b |
26 | |
f63e6310 |
27 | sub has_option_type { |
365e5784 |
28 | my (undef, $type_or_name) = @_; |
f63e6310 |
29 | |
c5c99e6b |
30 | return 1 if exists $option_type_map{ blessed $type_or_name |
31 | ? $type_or_name->name |
32 | : $type_or_name }; |
365e5784 |
33 | |
c5c99e6b |
34 | my $current = blessed $type_or_name |
35 | ? $type_or_name |
36 | : find_type_constraint($type_or_name); |
ac2073c8 |
37 | |
c5c99e6b |
38 | Moose->throw_error("Could not find the type constraint for '$type_or_name'") |
39 | unless defined $current; |
ac2073c8 |
40 | |
c5c99e6b |
41 | while ( my $parent = $current->parent ) { |
42 | return 1 if exists $option_type_map{ $parent->name }; |
f63e6310 |
43 | $current = $parent; |
c5c99e6b |
44 | }; |
45 | |
46 | return ''; |
47 | }; |
f63e6310 |
48 | |
f63e6310 |
49 | |
50 | sub get_option_type { |
365e5784 |
51 | my (undef, $type_or_name) = @_; |
52 | |
c5c99e6b |
53 | my $name = blessed $type_or_name ? $type_or_name->name : $type_or_name; |
f63e6310 |
54 | |
365e5784 |
55 | return $option_type_map{$name} if exists $option_type_map{$name}; |
56 | |
c5c99e6b |
57 | my $current = ref $type_or_name |
58 | ? $type_or_name |
59 | : find_type_constraint($type_or_name); |
ac2073c8 |
60 | |
c5c99e6b |
61 | Moose->throw_error("Could not find the type constraint for '$type_or_name'") |
62 | unless defined $current; |
365e5784 |
63 | |
64 | while ( $current = $current->parent ) { |
c5c99e6b |
65 | return $option_type_map{ $current->name } |
66 | if exists $option_type_map{ $current->name }; |
67 | }; |
f63e6310 |
68 | |
69 | return; |
c5c99e6b |
70 | }; |
71 | |
f63e6310 |
72 | |
8034a232 |
73 | sub add_option_type_to_map { |
74 | my (undef, $type_name, $option_string) = @_; |
365e5784 |
75 | |
c5c99e6b |
76 | Moose->throw_error("You must supply both a type name and an option string") |
77 | unless defined $type_name && defined $option_string; |
78 | |
79 | if (blessed $type_name) { |
365e5784 |
80 | $type_name = $type_name->name; |
365e5784 |
81 | } |
c5c99e6b |
82 | else { |
83 | Moose->throw_error("The type constraint '$type_name' does not exist") |
84 | unless find_type_constraint($type_name); |
85 | }; |
365e5784 |
86 | |
8034a232 |
87 | $option_type_map{$type_name} = $option_string; |
c5c99e6b |
88 | }; |
89 | |
8034a232 |
90 | |
91 | no Moose; no Moose::Util::TypeConstraints; 1; |
92 | |
8034a232 |
93 | |
c5c99e6b |
94 | __END__ |
8034a232 |
95 | |
96 | =pod |
97 | |
98 | =head1 NAME |
99 | |
100 | MooseX::Getopt::OptionTypeMap - Storage for the option to type mappings |
101 | |
102 | =head1 DESCRIPTION |
103 | |
104 | See the I<Custom Type Constraints> section in the L<MooseX::Getopt> docs |
105 | for more info about how to use this module. |
106 | |
107 | =head1 METHODS |
108 | |
109 | These are all class methods and should be called as such. |
110 | |
111 | =over 4 |
112 | |
365e5784 |
113 | =item B<has_option_type ($type_or_name)> |
8034a232 |
114 | |
365e5784 |
115 | =item B<get_option_type ($type_or_name)> |
8034a232 |
116 | |
117 | =item B<add_option_type_to_map ($type_name, $option_spec)> |
118 | |
edfb736c |
119 | =item B<meta> |
120 | |
8034a232 |
121 | =back |
122 | |
123 | =head1 BUGS |
124 | |
ac2073c8 |
125 | All complex software has bugs lurking in it, and this module is no |
8034a232 |
126 | exception. If you find a bug please either email me, or add the bug |
127 | to cpan-RT. |
128 | |
129 | =head1 AUTHOR |
130 | |
131 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
132 | |
133 | =head1 COPYRIGHT AND LICENSE |
134 | |
adbe3e57 |
135 | Copyright 2007-2008 by Infinity Interactive, Inc. |
8034a232 |
136 | |
137 | L<http://www.iinteractive.com> |
138 | |
139 | This library is free software; you can redistribute it and/or modify |
140 | it under the same terms as Perl itself. |
141 | |
f63e6310 |
142 | =cut |