module FoodFishParser::Flat::FishName

Constants

REGEX
REGEX_ALLERGEN
REGEX_ALLERGEN_NAMES

Public Class Methods

find_all(text) click to toggle source
# File lib/food_fish_parser/flat/fish_name.rb, line 19
def self.find_all(text)
  # Because scan doesn't support named captures, we have to use numbered capture groups.
  # Make sure to keep all groups you don't want to reference below as non-capturing groups.
  # Each name regex has a capture group (so as to avoid noise), so you don't see them here.
  # The order of the captures corresponds to the order of the fish names in the regex above.
  text.scan(REGEX).map do |m|
    case
    when m[0] && m[1] then { common: m[0], latin: m[1] }
    when m[2] && m[3] then { common: m[2], latin: m[3] }
    when m[4]         then { common: nil,  latin: m[4] }
    when m[5]         then { common: m[5], latin: nil  }
    end
  end.compact
end