2 package MooseX::Getopt::OptionTypeMap;
4 use Moose 'confess', 'blessed';
5 use Moose::Util::TypeConstraints 'find_type_constraint';
8 our $AUTHORITY = 'cpan:STEVAN';
11 my %option_type_map = (
18 'Defined|Str' => ':s',
19 'Defined|Int' => ':i',
20 'Defined|Num' => ':f',
21 'Str|Defined' => ':s',
22 'Int|Defined' => ':i',
23 'Num|Defined' => ':f',
28 my (undef, $type_or_name) = @_;
30 return 1 if exists $option_type_map{ blessed $type_or_name
34 my $current = blessed $type_or_name
36 : find_type_constraint($type_or_name);
38 Moose->throw_error("Could not find the type constraint for '$type_or_name'")
39 unless defined $current;
41 while ( my $parent = $current->parent ) {
42 return 1 if exists $option_type_map{ $parent->name };
51 my (undef, $type_or_name) = @_;
53 my $name = blessed $type_or_name ? $type_or_name->name : $type_or_name;
55 return $option_type_map{$name} if exists $option_type_map{$name};
57 my $current = ref $type_or_name
59 : find_type_constraint($type_or_name);
61 Moose->throw_error("Could not find the type constraint for '$type_or_name'")
62 unless defined $current;
64 while ( $current = $current->parent ) {
65 return $option_type_map{ $current->name }
66 if exists $option_type_map{ $current->name };
73 sub add_option_type_to_map {
74 my (undef, $type_name, $option_string) = @_;
76 Moose->throw_error("You must supply both a type name and an option string")
77 unless defined $type_name && defined $option_string;
79 if (blessed $type_name) {
80 $type_name = $type_name->name;
83 Moose->throw_error("The type constraint '$type_name' does not exist")
84 unless find_type_constraint($type_name);
87 $option_type_map{$type_name} = $option_string;
91 no Moose; no Moose::Util::TypeConstraints; 1;
100 MooseX::Getopt::OptionTypeMap - Storage for the option to type mappings
104 See the I<Custom Type Constraints> section in the L<MooseX::Getopt> docs
105 for more info about how to use this module.
109 These are all class methods and should be called as such.
113 =item B<has_option_type ($type_or_name)>
115 =item B<get_option_type ($type_or_name)>
117 =item B<add_option_type_to_map ($type_name, $option_spec)>
125 All complex software has bugs lurking in it, and this module is no
126 exception. If you find a bug please either email me, or add the bug
131 Stevan Little E<lt>stevan@iinteractive.comE<gt>
133 =head1 COPYRIGHT AND LICENSE
135 Copyright 2007-2008 by Infinity Interactive, Inc.
137 L<http://www.iinteractive.com>
139 This library is free software; you can redistribute it and/or modify
140 it under the same terms as Perl itself.