Revision bc3a01ae
Von Moritz Bunkus vor fast 12 Jahren hinzugefügt
SL/DB/Helper/Mappings.pm | ||
---|---|---|
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);
|
||
... | ... | |
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';
|
SL/Util.pm | ||
---|---|---|
|
||
use Carp;
|
||
|
||
our @EXPORT_OK = qw(_hashify);
|
||
our @EXPORT_OK = qw(_hashify camelify snakify);
|
||
|
||
sub _hashify {
|
||
my $keep = shift;
|
||
... | ... | |
((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__
|
||
|
||
... | ... | |
# 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
|
t/helper/camelify.t | ||
---|---|---|
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');
|
t/helper/snakify.t | ||
---|---|---|
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');
|
Auch abrufbar als: Unified diff
Funktionen 'snakify' und 'camelify' nach SL::Util verschoben, gebugfixt, getestet