Class: Smartdict::Drivers::AbstractDriver
- Inherits:
-
Object
- Object
- Smartdict::Drivers::AbstractDriver
- 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
Instance Attribute Summary collapse
-
#from_lang ⇒ Object
readonly
Returns the value of attribute from_lang.
-
#to_lang ⇒ Object
readonly
Returns the value of attribute to_lang.
-
#transcription ⇒ Object
Returns the value of attribute transcription.
-
#translated ⇒ Object
Returns the value of attribute translated.
-
#word ⇒ Object
readonly
Returns the value of attribute word.
Class Method Summary collapse
-
.set_name(name) ⇒ Object
Sets driver name.
- .translate(*args) ⇒ Object
Instance Method Summary collapse
- #build_translation ⇒ Object
-
#initialize(word, from_lang, to_lang) ⇒ AbstractDriver
constructor
A new instance of AbstractDriver.
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_lang ⇒ Object (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_lang ⇒ Object (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 |
#transcription ⇒ Object
Returns the value of attribute transcription.
27 28 29 |
# File 'lib/smartdict/drivers/abstract_driver.rb', line 27 def transcription @transcription end |
#translated ⇒ Object
Returns the value of attribute translated.
27 28 29 |
# File 'lib/smartdict/drivers/abstract_driver.rb', line 27 def translated @translated end |
#word ⇒ Object (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_translation ⇒ Object
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 |