Funktionen 'snakify' und 'camelify' nach SL::Util verschoben, gebugfixt, getestet
authorMoritz Bunkus <m.bunkus@linet-services.de>
Tue, 30 Apr 2013 14:10:03 +0000 (16:10 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Tue, 30 Apr 2013 15:21:12 +0000 (17:21 +0200)
SL/DB/Helper/Mappings.pm
SL/Util.pm
t/helper/camelify.t [new file with mode: 0644]
t/helper/snakify.t [new file with mode: 0644]

index 35845a8..cc4d2ab 100644 (file)
@@ -3,6 +3,8 @@ package SL::DB::Helper::Mappings;
 use utf8;
 use strict;
 
+use SL::Util qw(camelify);
+
 require Exporter;
 our @ISA       = qw(Exporter);
 our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
@@ -157,18 +159,6 @@ sub db {
   die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
 }
 
-sub camelify {
-  my ($str) = @_;
-  $str =~ s/_+(.)/uc($1)/ge;
-  ucfirst $str;
-}
-
-sub snakify {
-  my ($str) = @_;
-  $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
-  lcfirst $str;
-}
-
 sub plurify {
   my ($str) = @_;
   $str . 's';
index fdcc080..2fdd9c1 100644 (file)
@@ -6,7 +6,7 @@ use parent qw(Exporter);
 
 use Carp;
 
-our @EXPORT_OK = qw(_hashify);
+our @EXPORT_OK = qw(_hashify camelify snakify);
 
 sub _hashify {
   my $keep = shift;
@@ -18,6 +18,19 @@ sub _hashify {
           ((1 + $keep) == scalar(@_)) && ((ref($_[$keep]) || '') eq 'HASH') ? %{ $_[$keep] } : @_[$keep..scalar(@_) - 1]);
 }
 
+sub camelify {
+  my ($str) = @_;
+  $str =~ s/_+([[:lower:]])/uc($1)/ge;
+  ucfirst $str;
+}
+
+sub snakify {
+  my ($str) = @_;
+  $str =~ s/_([[:upper:]])/'_' . lc($1)/ge;
+  $str =~ s/(?<!^)([[:upper:]])/'_' . lc($1)/ge;
+  lc $str;
+}
+
 1;
 __END__
 
@@ -61,6 +74,22 @@ Template code both. Example:
     # Now do stuff, obviously!
   }
 
+=item C<camilify $string>
+
+Returns C<$string> converted from underscore-style to
+camel-case-style, e.g. for the string C<stupid_example_dude> it will
+return C<StupidExampleDude>.
+
+L</snakify> does the reverse.
+
+=item C<snakify $string>
+
+Returns C<$string> converted from camel-case-style to
+underscore-style, e.g. for the string C<EvenWorseExample> it will
+return C<even_worse_example>.
+
+L</camilify> does the reverse.
+
 =back
 
 =head1 BUGS
diff --git a/t/helper/camelify.t b/t/helper/camelify.t
new file mode 100644 (file)
index 0000000..0b5f8e3
--- /dev/null
@@ -0,0 +1,16 @@
+use Test::More tests => 8;
+
+use strict;
+
+use lib 't';
+
+use SL::Util qw(camelify);
+
+is(camelify('hello'),                'Hello',             'hello');
+is(camelify('hello_world'),          'HelloWorld',        'hello_world');
+is(camelify('hello_world_'),         'HelloWorld_',       'hello_world_');
+is(camelify('charlie_the_unicorn'),  'CharlieTheUnicorn', 'charlie_the_unicorn');
+is(camelify('_charlie_the_unicorn'), 'CharlieTheUnicorn', '_charlie_the_unicorn');
+is(camelify('hello__world'),         'HelloWorld',        'hello__world');
+is(camelify('hELLO'),                'HELLO',             'hELLO');
+is(camelify('hellO_worlD'),          'HellOWorlD',        'hellO_worlD');
diff --git a/t/helper/snakify.t b/t/helper/snakify.t
new file mode 100644 (file)
index 0000000..2a301bf
--- /dev/null
@@ -0,0 +1,15 @@
+use Test::More tests => 7;
+
+use strict;
+
+use lib 't';
+
+use SL::Util qw(snakify);
+
+is(snakify('Hello'),              'hello',                'Hello');
+is(snakify('HelloWorld'),         'hello_world',          'helloWorld');
+is(snakify('HelloWorld_'),        'hello_world_',         'helloWorld_');
+is(snakify('charlieTheUnicorn'),  'charlie_the_unicorn',  'charlieTheUnicorn');
+is(snakify('_CharlieTheUnicorn'), '_charlie_the_unicorn', '_CharlieTheUnicorn');
+is(snakify('HEllo'),              'h_ello',               'HEllo');
+is(snakify('HELlo'),              'h_e_llo',              'HELlo');