File Coverage

blib/lib/MooseX/Getopt/OptionTypeMap.pm
Criterion Covered Total %
statement 43 48 89.5
branch 22 34 64.7
condition 5 9 55.5
subroutine 8 8 100.0
pod 3 3 100.0
total 81 102 79.4


line stmt bran cond sub pod time code
1             package MooseX::Getopt::OptionTypeMap;
2             # ABSTRACT: Storage for the option to type mappings
3              
4             our $VERSION = '0.75';
5              
6 27     27   221 use Moose;
  27         67  
  27         220  
7 27     27   184352 use Carp 'confess';
  27         81  
  27         1851  
8 27     27   180 use Scalar::Util 'blessed';
  27         402  
  27         1497  
9 27     27   233 use Moose::Util::TypeConstraints 'find_type_constraint';
  27         63  
  27         318  
10 27     27   13354 use namespace::autoclean;
  27         8961  
  27         220  
11              
12             my %option_type_map = (
13             'Bool' => '!',
14             'Str' => '=s',
15             'Int' => '=i',
16             'Num' => '=f',
17             'ArrayRef' => '=s@',
18             'HashRef' => '=s%',
19             );
20              
21             sub has_option_type {
22 492     492 1 961 my (undef, $type_or_name) = @_;
23              
24 492 100 66     4192 if (blessed($type_or_name)
25             && $type_or_name->isa('Moose::Meta::TypeConstraint::Union')) {
26 1         2 foreach my $union_type (@{$type_or_name->type_constraints}) {
  1         46  
27 1 50       16 return 1
28             if __PACKAGE__->has_option_type($union_type);
29             }
30 0         0 return 0;
31             }
32              
33 491 50       13848 return 1 if exists $option_type_map{blessed($type_or_name) ? $type_or_name->name : $type_or_name};
    100          
34              
35 2 50       23 my $current = blessed($type_or_name) ? $type_or_name : find_type_constraint($type_or_name);
36              
37 2 50       6 (defined $current)
38             || confess "Could not find the type constraint for '$type_or_name'";
39              
40 2         58 while (my $parent = $current->parent) {
41 2 50       78 return 1 if exists $option_type_map{$parent->name};
42 0         0 $current = $parent;
43             }
44              
45 0         0 return 0;
46             }
47              
48             sub get_option_type {
49 492     492 1 874 my (undef, $type_or_name) = @_;
50              
51 492 100 66     2763 if (blessed($type_or_name)
52             && $type_or_name->isa('Moose::Meta::TypeConstraint::Union')) {
53 1         3 foreach my $union_type (@{$type_or_name->type_constraints}) {
  1         37  
54 1         12 my $option_type = __PACKAGE__->get_option_type($union_type);
55 1 50       5 return $option_type
56             if defined $option_type;
57             }
58 0         0 return;
59             }
60              
61 491 50       12686 my $name = blessed($type_or_name) ? $type_or_name->name : $type_or_name;
62              
63 491 100       3967 return $option_type_map{$name} if exists $option_type_map{$name};
64              
65 2 50       8 my $current = ref $type_or_name ? $type_or_name : find_type_constraint($type_or_name);
66              
67 2 50       6 (defined $current)
68             || confess "Could not find the type constraint for '$type_or_name'";
69              
70 2         55 while ( $current = $current->parent ) {
71             return $option_type_map{$current->name}
72 2 50       75 if exists $option_type_map{$current->name};
73             }
74              
75 0         0 return;
76             }
77              
78             sub add_option_type_to_map {
79 29     29 1 23807 my (undef, $type_name, $option_string) = @_;
80 29 50 33     147 (defined $type_name && defined $option_string)
81             || confess "You must supply both a type name and an option string";
82              
83 29 100       98 if ( blessed($type_name) ) {
84 24         121 $type_name = $type_name->name;
85             } else {
86 5 50       32 (find_type_constraint($type_name))
87             || confess "The type constraint '$type_name' does not exist";
88             }
89              
90 29         15413 $option_type_map{$type_name} = $option_string;
91             }
92              
93             1;
94              
95             __END__
96              
97             =pod
98              
99             =encoding UTF-8
100              
101             =head1 NAME
102              
103             MooseX::Getopt::OptionTypeMap - Storage for the option to type mappings
104              
105             =head1 VERSION
106              
107             version 0.75
108              
109             =head1 DESCRIPTION
110              
111             See the I<Custom Type Constraints> section in the L<MooseX::Getopt> docs
112             for more info about how to use this module.
113              
114             =head1 METHODS
115              
116             =head2 B<has_option_type ($type_or_name)>
117              
118             =head2 B<get_option_type ($type_or_name)>
119              
120             =head2 B<add_option_type_to_map ($type_name, $option_spec)>
121              
122             =head1 SUPPORT
123              
124             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Getopt>
125             (or L<bug-MooseX-Getopt@rt.cpan.org|mailto:bug-MooseX-Getopt@rt.cpan.org>).
126              
127             There is also a mailing list available for users of this distribution, at
128             L<http://lists.perl.org/list/moose.html>.
129              
130             There is also an irc channel available for users of this distribution, at
131             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
132              
133             =head1 AUTHOR
134              
135             Stevan Little <stevan@iinteractive.com>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2007 by Infinity Interactive, Inc.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut