class String::Matchable::Matcher

Attributes

data[R]

Public Class Methods

new() click to toggle source
# File lib/omu_support/core_ext/string.rb, line 68
def initialize
  @data = {}
end

Public Instance Methods

match(language:, category:, word:) click to toggle source
# File lib/omu_support/core_ext/string.rb, line 80
def match(language:, category:, word:)
  words(language, category).include? word
end
run(languages:, category:, word:) click to toggle source
# File lib/omu_support/core_ext/string.rb, line 72
def run(languages:, category:, word:)
  (languages.empty? ? %w[en tr] : languages.uniq).each do |language|
    return true if match(language: language, category: category, word: word)
  end

  false
end

Private Instance Methods

language_data_for(language) click to toggle source
# File lib/omu_support/core_ext/string.rb, line 93
def language_data_for(language)
  data[language = language.to_s] ||= begin
    (file = language_file_for(language)) ? YAML.load_file(file) : {}
  end
end
language_file_for(language) click to toggle source
# File lib/omu_support/core_ext/string.rb, line 99
def language_file_for(language)
  @lookup ||= Hash[*
    Dir[File.join File.expand_path('../data', __dir__), '*.yml'].map do |file|
      name = File.basename(file, '.yml')
      [name, file]
    end.flatten!
  ]
  @lookup[language]
end
words(language, category) click to toggle source

We should be lazy on all operations involving data access (hence the heavy use of or-equals operators).

# File lib/omu_support/core_ext/string.rb, line 89
def words(language, category)
  language_data_for(language)[category.to_s] ||= Set.new []
end