File Coverage

blib/lib/MouseX/Getopt/OptionTypeMap.pm
Criterion Covered Total %
statement 30 34 88.2
branch 15 26 57.6
condition 1 3 33.3
subroutine 7 7 100.0
pod 3 3 100.0
total 56 73 76.7


line stmt bran cond sub pod time code
1             package MouseX::Getopt::OptionTypeMap;
2             # ABSTRACT: Storage for the option to type mappings
3              
4 20     20   133 use Mouse 'confess', 'blessed';
  20         40  
  20         136  
5 20     20   7241 use Mouse::Util::TypeConstraints 'find_type_constraint';
  20         37  
  20         418  
6              
7             my %option_type_map = (
8             'Bool' => '!',
9             'Str' => '=s',
10             'Int' => '=i',
11             'Num' => '=f',
12             'ArrayRef' => '=s@',
13             'HashRef' => '=s%',
14             );
15              
16             sub has_option_type {
17 498     498 1 848 my (undef, $type_or_name) = @_;
18              
19 498 50       4308 return 1 if exists $option_type_map{blessed($type_or_name) ? $type_or_name->name : $type_or_name};
    100          
20              
21 2 50       8 my $current = blessed($type_or_name) ? $type_or_name : find_type_constraint($type_or_name);
22              
23 2 50       7 (defined $current)
24             || confess "Could not find the type constraint for '$type_or_name'";
25              
26 2         13 while (my $parent = $current->parent) {
27 2 50       12 return 1 if exists $option_type_map{$parent->name};
28 0         0 $current = $parent;
29             }
30              
31 0         0 return 0;
32             }
33              
34             sub get_option_type {
35 498     498 1 1936 my (undef, $type_or_name) = @_;
36              
37 498 50       1968 my $name = blessed($type_or_name) ? $type_or_name->name : $type_or_name;
38              
39 498 100       2645 return $option_type_map{$name} if exists $option_type_map{$name};
40              
41 2 50       7 my $current = ref $type_or_name ? $type_or_name : find_type_constraint($type_or_name);
42              
43 2 50       6 (defined $current)
44             || confess "Could not find the type constraint for '$type_or_name'";
45              
46 2         9 while ( $current = $current->parent ) {
47 2 50       18 return $option_type_map{$current->name}
48             if exists $option_type_map{$current->name};
49             }
50              
51 0         0 return;
52             }
53              
54             sub add_option_type_to_map {
55 1     1 1 1970 my (undef, $type_name, $option_string) = @_;
56 1 50 33     10 (defined $type_name && defined $option_string)
57             || confess "You must supply both a type name and an option string";
58              
59 1 50       5 if ( blessed($type_name) ) {
60 0         0 $type_name = $type_name->name;
61             } else {
62 1 50       5 (find_type_constraint($type_name))
63             || confess "The type constraint '$type_name' does not exist";
64             }
65              
66 1         19 $option_type_map{$type_name} = $option_string;
67             }
68              
69 20     20   10847 no Mouse::Util::TypeConstraints;
  20         42  
  20         133  
70 20     20   1492 no Mouse;
  20         43  
  20         90  
71              
72             1;
73              
74             =head1 DESCRIPTION
75              
76             See the I section in the L docs
77             for more info about how to use this module.
78              
79             =over 4
80              
81             =item B
82              
83             =item B
84              
85             =item B
86              
87             =back
88              
89             =cut