Class: Ingreedy::UnitVariationMapper
- Inherits:
-
Object
- Object
- Ingreedy::UnitVariationMapper
- Defined in:
- lib/ingreedy/unit_variation_mapper.rb
Class Method Summary collapse
Class Method Details
.all_variations ⇒ Object
9 10 11 12 13 14 |
# File 'lib/ingreedy/unit_variation_mapper.rb', line 9 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 ⇒ Object
4 5 6 7 |
# File 'lib/ingreedy/unit_variation_mapper.rb', line 4 def self.regexp regexp = all_variations.map { |v| Regexp.escape(v) }.join('|') regexp = Regexp.new(regexp, Regexp::IGNORECASE) end |
.unit_from_variation(variation) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ingreedy/unit_variation_mapper.rb', line 16 def self.unit_from_variation(variation) return if variations_map.empty? hash_entry_as_array = variations_map.detect { |unit, variations| variations.include?(variation) } if hash_entry_as_array hash_entry_as_array.first else # try again with the variation downcased # this is a hacky way to deal with the abbreviations for teaspoon and tablespoon hash_entry_as_array = variations_map.detect { |unit, variations| variations.include?(variation.downcase) } hash_entry_as_array.first end end |