Skip to content

Commit

Permalink
Make _normal2Camel recurse as well
Browse files Browse the repository at this point in the history
_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
afresh1 committed Oct 13, 2012
1 parent 3aad401 commit 83cb6ee
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/Transmission/AttributeRole.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,31 +63,38 @@ has eager_read => (
); );


# this method name exists to prove a point - not to be readable... # this method name exists to prove a point - not to be readable...
sub _camel2Normal { sub _convert {
if(ref $_[1] eq 'HASH') { if(ref $_[1] eq 'HASH') {
for my $camel (keys %{ $_[1] }) { for my $camel (keys %{ $_[1] }) {
my $key = __PACKAGE__->_camel2Normal($camel); my $key = $_[2]->($camel);


if(ref $_[1]->{$camel} eq 'HASH') { if(ref $_[1]->{$camel} eq 'HASH') {
__PACKAGE__->_camel2Normal($_[1]->{$camel}); __PACKAGE__->_convert($_[1]->{$camel}, $_[2]);
} }


$_[1]->{$key} = delete $_[1]->{$camel}; $_[1]->{$key} = delete $_[1]->{$camel};
} }
} }
else { else {
local $_ = $_[1]; return $_[2]->($_[1]);
}
}

sub _camel2Normal {
$_[0]->_convert( $_[1], sub {
local $_ = $_[0];
tr/-/_/; tr/-/_/;
s/([A-Z]+)/{ "_" .lc($1) }/ge; s/([A-Z]+)/{ "_" .lc($1) }/ge;
return $_; return $_;
} } );
} }

sub _normal2Camel { sub _normal2Camel {
local $_ = $_[1]; $_[0]->_convert( $_[1], sub {
tr/_/-/; local $_ = $_[0];
s/_(\w)/{ uc($1) }/ge; # wild guess... tr/_/-/;
return $_; s/_(\w)/{ uc($1) }/ge; # wild guess...
return $_;
} );
} }


=head1 LICENSE =head1 LICENSE
Expand Down

0 comments on commit 83cb6ee

Please sign in to comment.