Permalink
Browse files

Make _normal2Camel recurse as well

_camel2Normal would take a hashref as an argument, but I needed
hash keys to be able to go the other way as well.

At least it got less readable which appears to be a goal of this set of subs
  • Loading branch information...
1 parent 3aad401 commit 83cb6eed4e9557129510520f2f88aa9c1d17d0c6 @afresh1 committed Oct 13, 2012
Showing with 17 additions and 10 deletions.
  1. +17 −10 lib/Transmission/AttributeRole.pm
@@ -63,31 +63,38 @@ has eager_read => (
);
# this method name exists to prove a point - not to be readable...
-sub _camel2Normal {
+sub _convert {
if(ref $_[1] eq 'HASH') {
for my $camel (keys %{ $_[1] }) {
- my $key = __PACKAGE__->_camel2Normal($camel);
+ my $key = $_[2]->($camel);
if(ref $_[1]->{$camel} eq 'HASH') {
- __PACKAGE__->_camel2Normal($_[1]->{$camel});
+ __PACKAGE__->_convert($_[1]->{$camel}, $_[2]);
}
$_[1]->{$key} = delete $_[1]->{$camel};
}
}
else {
- local $_ = $_[1];
+ return $_[2]->($_[1]);
+ }
+}
+
+sub _camel2Normal {
+ $_[0]->_convert( $_[1], sub {
+ local $_ = $_[0];
tr/-/_/;
s/([A-Z]+)/{ "_" .lc($1) }/ge;
return $_;
- }
+ } );
}
-
sub _normal2Camel {
- local $_ = $_[1];
- tr/_/-/;
- s/_(\w)/{ uc($1) }/ge; # wild guess...
- return $_;
+ $_[0]->_convert( $_[1], sub {
+ local $_ = $_[0];
+ tr/_/-/;
+ s/_(\w)/{ uc($1) }/ge; # wild guess...
+ return $_;
+ } );
}
=head1 LICENSE

0 comments on commit 83cb6ee

Please sign in to comment.