Sha256: 142d0297e2b020b7d055ecda755042b2bf1aaaf6e35bef013d6237a081ed41e4

Contents?: true

Size: 952 Bytes

Versions: 1

Compression:

Stored size: 952 Bytes

Contents

class MemoryDictionary::Translator
  def initialize(dictionary_name)
    begin
      @dictionary = MemoryDictionary::Dictionary.find_by(name: dictionary_name)  
    rescue Mongoid::Errors::DocumentNotFound => e
      MemoryDictionary.logger.debug e
      raise MemoryDictionary::Errors::DictionaryNotFoundException
    end
  end

  def translate(word)
    begin
      word = @dictionary.words.find_by(name: word)
      word.translation
    rescue Mongoid::Errors::DocumentNotFound => e
      MemoryDictionary.logger.debug e
      raise MemoryDictionary::Errors::WordNotFoundException
    end
  end

  def add_word(word, translation, &block)
    @dictionary.words << MemoryDictionary::Word.new(name: word, translation: translation)
    block.call if block_given?
  end

  def self.add_word(dictionary, word, translation, &block)
    translator = MemoryDictionary::Translator.new(dictionary)
    translator.add_word(word, translation, &block)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
memory_dictionary-1.0.0 lib/memory_dictionary/translator.rb