-
-
Save caius/391113 to your computer and use it in GitHub Desktop.
This file contains 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
# | |
# Results: | |
# X adult(s), Y child(ren) and Z infant(s) | |
# | |
# rules: - | |
# - pluralization hacks in case of 1 of each of them. | |
# - only show children and infants if they're > 0 | |
# - at least one adult is always present | |
# | |
# examples... | |
# | |
# 5 adults, 2 children and 1 infant | |
# 1 adult and 2 infants | |
# 2 adults and 1 child | |
# 1 adult | |
def pdf_total_in_group opts={} | |
opts.default = 0 | |
# These could quite easily be other methods | |
pluralize = lambda {|number, single, plural| "#{number} #{number == 1 ? single : plural}" } | |
output_string = lambda {|number, single, plural| pluralize[number, single, plural] if number > 0 } | |
a = [ | |
output_string[opts[:adults], "adult", "adults"], | |
output_string[opts[:children], "child", "children"], | |
output_string[opts[:infants], "infant", "infants"], | |
].compact | |
a.size > 1 ? "#{a[0..-2].join(", ")} and #{a[-1]}" : a.first | |
end | |
pdf_total_in_group :adults => 5, :children => 2, :infants => 1 # => "5 adults, 2 children and 1 infant" | |
pdf_total_in_group :adults => 1, :infants => 2 # => "1 adult and 2 infants" | |
pdf_total_in_group :adults => 2, :children => 1 # => "2 adults and 1 child" | |
pdf_total_in_group :adults => 1 # => "1 adult" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment