blib/lib/Hash/KeyMorpher.pm

filter: Statementstmt 100% 36 / 36 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 98.4% 64 / 65 CC 6 SCAR 6.6
CC
6
Cov
98%
key_morph
4.0
_split_words
2.0
to_delim
2.0
CRAP 1.9
↑ Summary
1 package Hash::KeyMorpher;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Hash::KeyMorpher - Deep converter for naming conventions of hash keys
8  
9 =head1 DESCRIPTION
10  
11 Deeply change the nameing conventions for keys in hash structures, or simply change strings between naming conventions.
12 Converts to CamelCase, mixedCamel, delimited_string, UPPER, LOWER
13  
14  
15 =head1 SYNOPSYS
16  
17 use Hash::KeyMorpher; # import all, or
18 use Hash::KeyMorpher qw (key_morph to_camel to_mixed to_under to_delim); # import specific subs
19  
20 # To use the string converters:
21 $res = to_camel('my_string'); # MyString
22 $res = to_mixed('my_string'); # myString
23 $res = to_under('myString'); # my_string
24 $res = to_delim('myString','-'); # my-string
25  
26 # To morph keys in a hash, key_morph($hash,$method,$delim);
27 # method is one of camel,mixed,under,delim,upper,lower
28 $h1 = { 'level_one' => { 'LevelTwo' => 'foo' } };
29 $mixed = key_morph($h1,'mixed'); # { 'levelOne' => { 'levelTwo' => 'foo' } };
30 $delim = key_morph($h1,'delim','-'); # { 'level-one' => { 'level-two' => 'foo' } };
31
32 # To morph acceccor keys
33 $obj = Hash::Accessor->new(qw /CamelCase mixedCase delim_str UPPER lower/);
34 $camel = key_morph($obj,'camel');
35
36 =head1 EXPORT
37  
38 This module exports key_morph, to_camel, to_mixed, to_under and to_delim.
39 You will probably only need key_morph unless you really want the others.
40  
41  
42 =head1 FUNCTIONS
43  
44 =head2 _split_words($str)
45  
46 Splits a string into words, identifying boundaries using Capital Letters or Underscores etc.
47 This sub is not exported
48  
49 =head2 key_morph($hash,$method,$delim)
50  
51 $method can be one of (camel, mixed, delim, upper, lower).
52 $delim should be specified if using the delim method; by default its an empty string.
53  
54 =head2 to_camel($str)
55  
56 Convers string to CamelCase
57  
58 =head2 to_mixed($str)
59  
60 Convers string to mixedCamelCase
61  
62 =head2 to_under($str)
63  
64 Convers string to underscore_separated
65  
66 =head2 to_delim($str,$delim)
67  
68 Convers string to custom delimited-string (delimited by second parameter)
69  
70 =head2 to_upper($str)
71  
72 Returns the uppercase version of the rejoined string (removes undescores etc)
73  
74 =head2 to_lower($str)
75  
76 Returns the lowercase version of the rejoined string (removes undescores etc)
77  
78 =head1 AUTHOR AND SUPPORT
79  
80 Copyright (c) Michael Holloway 2013 , E<lt>michael@thedarkwinter.comE<gt>
81  
82 =head1 COPYRIGHT
83  
84 Perl Arstistic License
85  
86 =cut
87  
88 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 3 our $VERSION = '0.09';
92  
93 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
94 3 our @EXPORT = qw(to_mixed to_camel to_under to_delim key_morph);
95  
96 # warning, _split_words will return lower case! Should only be used internally which is why its not exported
97 68 ▶sub _split_words {
Subroutine _split_words: called CC=2 SCAR=6.9
98 68 my ($inp) = @_;
99 68 my @words = split( /(?<=[a-z])(?=[A-Z])|-|_/ , $inp);
100 68 ▶ return lc($words[0]) if $#words==0;
Branch: 1 branch
BranchTrueFalseTotal
if $#words == 0 17 51 68
101 51 return map { lc } @words;
102 }
103  
104 # string converters
105 5 ▶sub to_upper { return uc join '', _split_words(shift); }
Subroutine to_upper: called CC=1 SCAR=0.0
Pod documented
106 5 ▶sub to_lower { return lc join '', _split_words(shift); }
Subroutine to_lower: called CC=1 SCAR=0.0
Pod documented
107 16 ▶sub to_mixed { return lcfirst to_camel(shift); }
Subroutine to_mixed: called CC=1 SCAR=0.0
Pod documented
108 27 ▶sub to_camel { return join('', map{ ucfirst $_ } _split_words(shift)); }
Subroutine to_camel: called CC=1 SCAR=0.0
Pod documented
109 11 ▶sub to_under { return lc(join('_', map { $_ } _split_words($_[0]))); }
Subroutine to_under: called CC=1 SCAR=0.0
Pod documented
110 13 ▶sub to_delim { return lc(join( defined $_[1]?$_[1]:'', map { $_ } _split_words($_[0]))); }
Subroutine to_delim: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 12 1 13
111  
112 # recursively process hash
113 63 ▶sub key_morph {
Subroutine key_morph: called CC=4 SCAR=13.9
Pod documented
114 63 my ($inp,$sub,$delim) = @_;
115 63 my $disp = { upper => \&to_upper, lower => \&to_lower, mixed => \&to_mixed, camel => \&to_camel, under => \&to_under, delim => \&to_delim };
116 63 ▶ return $inp unless defined $disp->{$sub};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $disp->{$sub} 0 63 63
117
118 63 my $r = ref($inp);
119 #print "$inp ($r)\n";
120 63 ▶ return {map { $disp->{$sub}->($_,$delim) => key_morph($inp->{$_},$sub,$delim); } keys %$inp} if ($r eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "HASH" 28 35 63
121 35 ▶ return [ map key_morph($_,$sub,$delim), @$inp ] if ($r eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" 7 28 35
122
123 28 return $inp;
124 }
125  
126 3 1;
↑ Summary