Class: Smartdict::Drivers::AbstractDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/smartdict/drivers/abstract_driver.rb

Overview

Translation driver provides an ability to translate words using external data sources. It can be data from local disk, database or remote services like Google Translate. Every driver must inherit Smartdict::Driver and have implementation of #translate method. This method should sets translated and transcription properties. For examples you can see #Smartdict::Driver::GoogleTranslateDriver.

In your implementation of #translate you need to use methods:

  • word - returns a word what needs to be translated.

  • from_lang - code of source language(“en”, “ru”, etc)

  • to_lang - code of target language(“en, ”ru, etc)

Usage:

class HelloWorldDriver < Smartdict::Driver
  # Set name of driver, so DriverManager can identify it.
  name "hello_world"

  # This trivial example will always return the same translation.
  def translate
    self.translated = {"noun" => ["hello", "hi"], "verb" => ["greet"]}
    self.transcription = "he'leu"
  end
end

Direct Known Subclasses

GoogleTranslateDriver, LingvoYandexDriver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word, from_lang, to_lang) ⇒ AbstractDriver

Returns a new instance of AbstractDriver.



41
42
43
44
45
46
47
# File 'lib/smartdict/drivers/abstract_driver.rb', line 41

def initialize(word, from_lang, to_lang)
  @word      = word
  @from_lang = from_lang.to_s
  @to_lang   = to_lang.to_s
  translate
  raise Smartdict::TranslationNotFound if(translated.nil? || translated.empty?)
end

Instance Attribute Details

#from_langObject (readonly)

Returns the value of attribute from_lang.



26
27
28
# File 'lib/smartdict/drivers/abstract_driver.rb', line 26

def from_lang
  @from_lang
end

#to_langObject (readonly)

Returns the value of attribute to_lang.



26
27
28
# File 'lib/smartdict/drivers/abstract_driver.rb', line 26

def to_lang
  @to_lang
end

#transcriptionObject

Returns the value of attribute transcription.



27
28
29
# File 'lib/smartdict/drivers/abstract_driver.rb', line 27

def transcription
  @transcription
end

#translatedObject

Returns the value of attribute translated.



27
28
29
# File 'lib/smartdict/drivers/abstract_driver.rb', line 27

def translated
  @translated
end

#wordObject (readonly)

Returns the value of attribute word.



26
27
28
# File 'lib/smartdict/drivers/abstract_driver.rb', line 26

def word
  @word
end

Class Method Details

.set_name(name) ⇒ Object

Sets driver name



37
38
39
# File 'lib/smartdict/drivers/abstract_driver.rb', line 37

def self.set_name(name)
  self.name = name.to_s
end

.translate(*args) ⇒ Object



32
33
34
# File 'lib/smartdict/drivers/abstract_driver.rb', line 32

def self.translate(*args)
  self.new(*args).build_translation
end

Instance Method Details

#build_translationObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smartdict/drivers/abstract_driver.rb', line 49

def build_translation
  translation = Smartdict::Translation.new(
    :word          => word,
    :from_lang     => from_lang.to_s,
    :to_lang       => to_lang.to_s,
    :transcription => transcription,
    :driver        => self.name
  )
  translated.each do |word_class, words|
    translation.translated[word_class] = words
  end
  translation
end