Class: WordSmith::Models::Word

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/models/word.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, word:, pronunciation:, meaning:, example:, context: nil, target_language: nil, translation_to_target_language: nil) ⇒ Word

Returns a new instance of Word.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/models/word.rb', line 41

def initialize(
  id:,
  word:,
  pronunciation:,
  meaning:,
  example:,
  context: nil,
  target_language: nil,
  translation_to_target_language: nil
)
  @id = id
  @word = word
  @pronunciation = pronunciation
  @meaning = meaning
  @example = example
  @context = context
  @target_language = target_language
  @translation_to_target_language = translation_to_target_language
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



28
29
30
# File 'lib/models/word.rb', line 28

def context
  @context
end

#exampleObject (readonly)

Returns the value of attribute example.



25
26
27
# File 'lib/models/word.rb', line 25

def example
  @example
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/models/word.rb', line 13

def id
  @id
end

#meaningObject (readonly)

Returns the value of attribute meaning.



22
23
24
# File 'lib/models/word.rb', line 22

def meaning
  @meaning
end

#pronunciationObject (readonly)

Returns the value of attribute pronunciation.



19
20
21
# File 'lib/models/word.rb', line 19

def pronunciation
  @pronunciation
end

#target_languageObject (readonly)

Returns the value of attribute target_language.



31
32
33
# File 'lib/models/word.rb', line 31

def target_language
  @target_language
end

#translation_to_target_languageObject (readonly)

Returns the value of attribute translation_to_target_language.



34
35
36
# File 'lib/models/word.rb', line 34

def translation_to_target_language
  @translation_to_target_language
end

#wordObject (readonly)

Returns the value of attribute word.



16
17
18
# File 'lib/models/word.rb', line 16

def word
  @word
end

Class Method Details

.allObject



92
93
94
95
96
97
# File 'lib/models/word.rb', line 92

def all
  Services::DB.instance.execute('SELECT id, word, pronunciation, meaning, example, context, target_language, translation_to_target_language FROM words').map do |row|
    new(id: row[0], word: row[1], pronunciation: row[2], meaning: row[3], example: row[4], context: row[5],
        target_language: row[6], translation_to_target_language: row[7])
  end
end

.create(word:, pronunciation:, meaning:, example:, context: nil, target_language: nil, translation_to_target_language: nil) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/models/word.rb', line 104

def create(word:, pronunciation:, meaning:, example:, context: nil, target_language: nil,
           translation_to_target_language: nil)
  result = Services::DB.instance.execute('INSERT INTO words (word, pronunciation, meaning, example, context, target_language, translation_to_target_language) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *',
                                         [word, pronunciation, meaning, example, context, target_language,
                                          translation_to_target_language]).first

  new(id: result[0], word: result[1], pronunciation: result[2], meaning: result[3], example: result[4],
      context: result[5], target_language: result[6], translation_to_target_language: result[7])
end

.find(id) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/models/word.rb', line 115

def find(id)
  Services::DB.instance.execute('SELECT id, word, pronunciation, meaning, example, context, target_language, translation_to_target_language FROM words WHERE id = ?',
                                [id]).map do |row|
    new(id: row[0], word: row[1], pronunciation: row[2], meaning: row[3], example: row[4], context: row[5],
        target_language: row[6], translation_to_target_language: row[7])
  end.first
end

.find_by_word(word) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/models/word.rb', line 124

def find_by_word(word)
  Services::DB.instance.execute('SELECT id, word, pronunciation, meaning, example, context, target_language, translation_to_target_language FROM words WHERE word = ?',
                                [word]).map do |row|
    new(id: row[0], word: row[1], pronunciation: row[2], meaning: row[3], example: row[4], context: row[5],
        target_language: row[6], translation_to_target_language: row[7])
  end.first
end

Instance Method Details

#deleteObject



62
63
64
# File 'lib/models/word.rb', line 62

def delete
  Services::DB.instance.execute('DELETE FROM words WHERE id = ?', [@id])
end

#update(word:, pronunciation:, meaning:, example:, context: nil, target_language: nil, translation_to_target_language: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/models/word.rb', line 71

def update(word:, pronunciation:, meaning:, example:, context: nil, target_language: nil,
           translation_to_target_language: nil)
  result = Services::DB.instance.execute('UPDATE words SET word = ?, pronunciation = ?, meaning = ?, example = ?, context = ?, target_language = ?, translation_to_target_language = ? WHERE id = ? RETURNING *',
                                         [word, pronunciation, meaning, example, context, target_language,
                                          translation_to_target_language, @id]).first

  @word = result[1]
  @pronunciation = result[2]
  @meaning = result[3]
  @example = result[4]
  @context = result[5]
  @target_language = result[6]
  @translation_to_target_language = result[7]

  self
end