Module: QuantityResolver

Included in:
CooklangRb::Tag
Defined in:
lib/cooklang_rb/quantity_resolver.rb

Instance Method Summary collapse

Instance Method Details

#resolve_quantity(quantity, default: "") ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cooklang_rb/quantity_resolver.rb', line 2

def resolve_quantity(quantity, default: "")
  return default if quantity.nil?
  return quantity if quantity.is_a? Numeric

  quantity.strip! if quantity.respond_to?(:strip!)

  if quantity.include?(".")
    quantity.to_f
  elsif quantity.include?("/") && quantity[0] != "0"
    quantity.gsub(/\s/, "").to_r.to_f
  elsif quantity.match?(/^[0-9]+$/)
    quantity.to_i
  else
    quantity.empty? ? default : quantity
  end
end