Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision bc3a01ae

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID bc3a01aef9a58d5e22137dbcc38fec7abebfdb22
  • Vorgänger fc1490e8
  • Nachfolger c8e0c77f

Funktionen 'snakify' und 'camelify' nach SL::Util verschoben, gebugfixt, getestet

Unterschiede anzeigen:

SL/DB/Helper/Mappings.pm
3 3
use utf8;
4 4
use strict;
5 5

  
6
use SL::Util qw(camelify);
7

  
6 8
require Exporter;
7 9
our @ISA       = qw(Exporter);
8 10
our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
......
157 159
  die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
158 160
}
159 161

  
160
sub camelify {
161
  my ($str) = @_;
162
  $str =~ s/_+(.)/uc($1)/ge;
163
  ucfirst $str;
164
}
165

  
166
sub snakify {
167
  my ($str) = @_;
168
  $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
169
  lcfirst $str;
170
}
171

  
172 162
sub plurify {
173 163
  my ($str) = @_;
174 164
  $str . 's';
SL/Util.pm
6 6

  
7 7
use Carp;
8 8

  
9
our @EXPORT_OK = qw(_hashify);
9
our @EXPORT_OK = qw(_hashify camelify snakify);
10 10

  
11 11
sub _hashify {
12 12
  my $keep = shift;
......
18 18
          ((1 + $keep) == scalar(@_)) && ((ref($_[$keep]) || '') eq 'HASH') ? %{ $_[$keep] } : @_[$keep..scalar(@_) - 1]);
19 19
}
20 20

  
21
sub camelify {
22
  my ($str) = @_;
23
  $str =~ s/_+([[:lower:]])/uc($1)/ge;
24
  ucfirst $str;
25
}
26

  
27
sub snakify {
28
  my ($str) = @_;
29
  $str =~ s/_([[:upper:]])/'_' . lc($1)/ge;
30
  $str =~ s/(?<!^)([[:upper:]])/'_' . lc($1)/ge;
31
  lc $str;
32
}
33

  
21 34
1;
22 35
__END__
23 36

  
......
61 74
    # Now do stuff, obviously!
62 75
  }
63 76

  
77
=item C<camilify $string>
78

  
79
Returns C<$string> converted from underscore-style to
80
camel-case-style, e.g. for the string C<stupid_example_dude> it will
81
return C<StupidExampleDude>.
82

  
83
L</snakify> does the reverse.
84

  
85
=item C<snakify $string>
86

  
87
Returns C<$string> converted from camel-case-style to
88
underscore-style, e.g. for the string C<EvenWorseExample> it will
89
return C<even_worse_example>.
90

  
91
L</camilify> does the reverse.
92

  
64 93
=back
65 94

  
66 95
=head1 BUGS
t/helper/camelify.t
1
use Test::More tests => 8;
2

  
3
use strict;
4

  
5
use lib 't';
6

  
7
use SL::Util qw(camelify);
8

  
9
is(camelify('hello'),                'Hello',             'hello');
10
is(camelify('hello_world'),          'HelloWorld',        'hello_world');
11
is(camelify('hello_world_'),         'HelloWorld_',       'hello_world_');
12
is(camelify('charlie_the_unicorn'),  'CharlieTheUnicorn', 'charlie_the_unicorn');
13
is(camelify('_charlie_the_unicorn'), 'CharlieTheUnicorn', '_charlie_the_unicorn');
14
is(camelify('hello__world'),         'HelloWorld',        'hello__world');
15
is(camelify('hELLO'),                'HELLO',             'hELLO');
16
is(camelify('hellO_worlD'),          'HellOWorlD',        'hellO_worlD');
t/helper/snakify.t
1
use Test::More tests => 7;
2

  
3
use strict;
4

  
5
use lib 't';
6

  
7
use SL::Util qw(snakify);
8

  
9
is(snakify('Hello'),              'hello',                'Hello');
10
is(snakify('HelloWorld'),         'hello_world',          'helloWorld');
11
is(snakify('HelloWorld_'),        'hello_world_',         'helloWorld_');
12
is(snakify('charlieTheUnicorn'),  'charlie_the_unicorn',  'charlieTheUnicorn');
13
is(snakify('_CharlieTheUnicorn'), '_charlie_the_unicorn', '_CharlieTheUnicorn');
14
is(snakify('HEllo'),              'h_ello',               'HEllo');
15
is(snakify('HELlo'),              'h_e_llo',              'HELlo');

Auch abrufbar als: Unified diff