Skip to content

Instantly share code, notes, and snippets.

@sshaw
Created July 12, 2012 00:18
Show Gist options
  • Save sshaw/3094651 to your computer and use it in GitHub Desktop.
Save sshaw/3094651 to your computer and use it in GitHub Desktop.
Single or Plural Count Helper for Mojolicious
# 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