Class: Smartdict::Models::Translation

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, Smartdict::Models
Defined in:
lib/smartdict/models/translation.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Translation

TODO: it’s a hack which necessary because of weird DM bugs.



64
65
66
67
68
# File 'lib/smartdict/models/translation.rb', line 64

def initialize(*args)
  self.class.finalize
  self.word_class_id = 1
  super(*args)
end

Class Method Details

.create_from_struct(struct) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smartdict/models/translation.rb', line 26

def self.create_from_struct(struct)
  from_lang = Language[struct.from_lang]
  to_lang   = Language[struct.to_lang]
  driver    = Driver[struct.driver]

  word = Word.first_or_create(:name => struct.word, :language_id => from_lang.id)
  word.save!

  translation = self.create(
    :word          => word,
    :driver        => driver,
    :from_lang_id  => from_lang.id,
    :to_lang_id    => to_lang.id,
    :transcription => struct.transcription
  )

  struct.translated.each do |word_class_name, meanings|
    meanings.each do |meaning|
      w = Word.first_or_create(:name => meaning, :language_id => to_lang.id)
      word_class = WordClass[word_class_name]
      TranslatedWord.create(:word => w, :word_class => word_class, :translation => translation)
    end
  end

  translation
end

.find(word, from_lang_code, to_lang_code, driver_name) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/smartdict/models/translation.rb', line 53

def self.find(word, from_lang_code, to_lang_code, driver_name)
  from_lang = Language[from_lang_code]
  to_lang   = Language[to_lang_code]
  driver    = Driver[driver_name]

  word = Word.first(:name => word, :language_id => from_lang.id)
  self.first(:from_lang => from_lang, :to_lang => to_lang, :word => word, :driver => driver)
end

Instance Method Details

#to_structObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/smartdict/models/translation.rb', line 70

def to_struct
  struct = Smartdict::Translation.new(
    :word          => word.name,
    :transcription => self.transcription,
    :from_lang     => from_lang.code,
    :to_lang       => to_lang.code,
    :driver        => driver.name
  )
  translated_words.each do |tw|
    struct[tw.word_class.name] << tw.word.name
  end
  struct
end