1 package MooseX::Getopt::OptionTypeMap;
2 # ABSTRACT: Storage for the option to type mappings
4 use Moose 'confess', 'blessed';
5 use Moose::Util::TypeConstraints 'find_type_constraint';
7 my %option_type_map = (
17 my (undef, $type_or_name) = @_;
19 if (blessed($type_or_name)
20 && $type_or_name->isa('Moose::Meta::TypeConstraint::Union')) {
21 foreach my $union_type (@{$type_or_name->type_constraints}) {
23 if __PACKAGE__->has_option_type($union_type);
28 return 1 if exists $option_type_map{blessed($type_or_name) ? $type_or_name->name : $type_or_name};
30 my $current = blessed($type_or_name) ? $type_or_name : find_type_constraint($type_or_name);
33 || confess "Could not find the type constraint for '$type_or_name'";
35 while (my $parent = $current->parent) {
36 return 1 if exists $option_type_map{$parent->name};
44 my (undef, $type_or_name) = @_;
46 if (blessed($type_or_name)
47 && $type_or_name->isa('Moose::Meta::TypeConstraint::Union')) {
48 foreach my $union_type (@{$type_or_name->type_constraints}) {
49 my $option_type = __PACKAGE__->get_option_type($union_type);
51 if defined $option_type;
56 my $name = blessed($type_or_name) ? $type_or_name->name : $type_or_name;
58 return $option_type_map{$name} if exists $option_type_map{$name};
60 my $current = ref $type_or_name ? $type_or_name : find_type_constraint($type_or_name);
63 || confess "Could not find the type constraint for '$type_or_name'";
65 while ( $current = $current->parent ) {
66 return $option_type_map{$current->name}
67 if exists $option_type_map{$current->name};
73 sub add_option_type_to_map {
74 my (undef, $type_name, $option_string) = @_;
75 (defined $type_name && defined $option_string)
76 || confess "You must supply both a type name and an option string";
78 if ( blessed($type_name) ) {
79 $type_name = $type_name->name;
81 (find_type_constraint($type_name))
82 || confess "The type constraint '$type_name' does not exist";
85 $option_type_map{$type_name} = $option_string;
88 no Moose::Util::TypeConstraints;
95 See the I<Custom Type Constraints> section in the L<MooseX::Getopt> docs
96 for more info about how to use this module.
98 =method B<has_option_type ($type_or_name)>
100 =method B<get_option_type ($type_or_name)>
102 =method B<add_option_type_to_map ($type_name, $option_spec)>