Created
July 12, 2012 00:18
-
-
Save sshaw/3094651 to your computer and use it in GitHub Desktop.
Single or Plural Count Helper for Mojolicious
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Single or Plural Count Helper for Mojolicious using Lingua::EN::Inflect::PL | |
# usage: | |
# $size = 10 | |
# count($size, 'user') | |
# 10 users | |
# | |
# $users = an array ref with one User object | |
# count($users) | |
# 1 User | |
# | |
# count($users, 'Luser') | |
# 1 Luser | |
$self->helper(count => sub { | |
my ($c, $item, $type) = @_; | |
my $count = $item; | |
my $tr = sub { ucfirst lc( (split /::/, ref(shift))[-1] ) }; | |
if(ref($item) eq 'ARRAY') { | |
$count = @$item; | |
$type = $tr->($item->[0]) unless $type; | |
} | |
$type ||= $tr->($item); | |
return "$count " . Lingua::EN::Inflect::PL($type, $count); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment