File Coverage

blib/lib/Glib/Ex/EnumBits.pm
Criterion Covered Total %
statement 37 52 71.1
branch 6 18 33.3
condition 2 6 33.3
subroutine 9 10 90.0
pod 3 3 100.0
total 57 89 64.0


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2014 Kevin Ryde
2              
3             # This file is part of Glib-Ex-ObjectBits.
4             #
5             # Glib-Ex-ObjectBits is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Glib-Ex-ObjectBits is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Glib-Ex-ObjectBits. If not, see .
17              
18             package Glib::Ex::EnumBits;
19 1     1   709 use 5.008;
  1         4  
  1         40  
20 1     1   6 use strict;
  1         2  
  1         37  
21 1     1   14 use warnings;
  1         2  
  1         41  
22 1     1   7 use Carp;
  1         1  
  1         75  
23              
24             # uncomment this to run the ### lines
25             #use Smart::Comments;
26              
27 1     1   5 use Exporter;
  1         1  
  1         164  
28             our @ISA = ('Exporter');
29             our @EXPORT_OK = qw(to_display
30             to_display_default
31             to_description);
32              
33             our $VERSION = 16;
34              
35             sub to_display {
36 12     12 1 4550 my ($enum_class, $nick) = @_;
37             ### EnumBits to_display(): "$enum_class $nick"
38              
39 12 50       26 if (@_ != 2) {
40 0         0 croak 'EnumBits to_display() wrong number of arguments';
41             }
42 12 50       60 if (my $coderef = $enum_class->can('EnumBits_to_display')) {
43             ### $coderef
44 0 0       0 if (defined (my $str = $enum_class->$coderef($nick))) {
45 0         0 return $str;
46             }
47             }
48 12 50       11 if (defined (my $str = do {
49 1     1   6 no strict 'refs';
  1         2  
  1         448  
50 12         10 ${"${enum_class}::EnumBits_to_display"}{$nick}
  12         43  
51             })) {
52 0         0 return $str;
53             }
54 12         18 return to_display_default ($enum_class, $nick);
55             }
56              
57             sub to_display_default {
58 24     24 1 3577 my ($enum_class, $nick) = @_;
59             ### EnumBits to_display_default(): $nick
60              
61 24 50       42 if (@_ != 2) {
62             # it's easy to forget the $enum_class parameter, guard against that ...
63 0         0 croak 'EnumBits to_display_default() wrong number of arguments';
64             }
65            
66 24 50       51 if ($nick =~ /^([-_ ]+)$/) {
67             # consists entirely of separators, eg "--"
68             # preserve something non-empty
69 0         0 $nick =~ s/^\s+//; # leading whitespace
70 0         0 $nick =~ s/\s+$//; # trailing whitespace
71 0         0 return $nick;
72             }
73              
74 60         124 my $str = join (' ',
75 24         207 map {ucfirst}
76             split(/[-_ ]+
77             |(?<=[^[:upper:][:digit:]])(?=\d) # before a digit
78             |(?<=\d)(?=\D) # after a digit
79             |(?<=[[:lower:]])(?=[[:upper:]])
80             |(?<=[[:upper:]])(?=[[:upper:]][[:lower:]])
81             /x,
82             $nick));
83 24 50 33     60 if (defined $enum_class
      33        
84             && defined (my $textdomain = do {
85 1     1   7 no strict 'refs';
  1         1  
  1         596  
86 24         20 ${"${enum_class}::EnumBits_textdomain"}
  24         121  
87             })
88             && Locale::Messages->can('dgettext')) {
89 0         0 $str = Locale::Messages::dgettext ($textdomain, $str);
90             }
91 24         62 return $str;
92             }
93              
94             sub to_description {
95 0     0 1   my ($enum_class, $nick) = @_;
96 0 0         if (@_ < 2) {
97 0           croak "Not enough arguments for EnumBits to_description()";
98             }
99 0 0         if (my $coderef = $enum_class->can('EnumBits_to_description')) {
100 0           return $enum_class->$coderef($nick);
101             }
102 0           return undef;
103             }
104              
105             1;
106             __END__