class Ingreedy::UnitVariationMapper

Public Class Methods

all_variations() click to toggle source
# File lib/ingreedyfork/unit_variation_mapper.rb, line 8
def self.all_variations
  # Return these in order of size, descending
  # That way, the longer versions will try to be parsed first,
  # then the shorter versions
  # e.g. so '1 cup flour' will be parsed as 'cup' instead of 'c'
  variations_map.values.flatten.sort { |a, b| b.length <=> a.length }
end
regexp() click to toggle source
# File lib/ingreedyfork/unit_variation_mapper.rb, line 3
def self.regexp
  regexp_string = all_variations.map { |v| Regexp.escape(v) }.join("|")
  Regexp.new(regexp_string, Regexp::IGNORECASE)
end
unit_from_variation(variation) click to toggle source
# File lib/ingreedyfork/unit_variation_mapper.rb, line 16
def self.unit_from_variation(variation)
  return if variations_map.empty?

  hash_entry_as_array = variations_map.detect do |_unit, variations|
    variations.include?(variation)
  end

  if hash_entry_as_array
    hash_entry_as_array.first
  else
    # try again with the variation downcased
    # (hack to deal with the abbreviations for teaspoon and tablespoon)
    hash_entry_as_array = variations_map.detect do |_unit, variations|
      variations.include?(variation.downcase)
    end
    hash_entry_as_array.first
  end
end
variations_map() click to toggle source
# File lib/ingreedyfork/unit_variation_mapper.rb, line 35
def self.variations_map
  Ingreedy.dictionaries.current.units
end