Class: Ingreedy::UnitVariationMapper
- Inherits:
-
Object
- Object
- Ingreedy::UnitVariationMapper
- Defined in:
- lib/ingreedy/unit_variation_mapper.rb
Class Method Summary collapse
- .all_variations ⇒ Object
- .regexp ⇒ Object
- .unit_from_variation(variation) ⇒ Object
- .variations_map ⇒ Object
Class Method Details
.all_variations ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/ingreedy/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 ⇒ Object
3 4 5 6 |
# File 'lib/ingreedy/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) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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 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 ⇒ Object
35 36 37 |
# File 'lib/ingreedy/unit_variation_mapper.rb', line 35 def self.variations_map Ingreedy.dictionaries.current.units end |