class StringToIpa::Phonetic

Attributes

id[R]
phonetic[RW]
word[RW]

Public Class Methods

find(s_id) click to toggle source
# File lib/string_to_ipa.rb, line 64
def self.find(s_id)
  result = database.execute("SELECT * FROM phonetics WHERE id = #{s_id}")[0]
  self.new(result)
end
new(options) click to toggle source
# File lib/string_to_ipa.rb, line 10
def initialize(options)
  @word = options[:word]
  @phonetic = options[:phonetic]
  @id = options[:id]
end

Public Instance Methods

delete() click to toggle source
# File lib/string_to_ipa.rb, line 60
def delete
  database.execute("DELETE FROM phonetics WHERE id = #{@id}")
end
insert() click to toggle source
# File lib/string_to_ipa.rb, line 36
def insert
  database.execute("INSERT INTO phonetics (word, phonetic) VALUES (?, ?)", @word, @phonetic)
  @id = database.last_insert_row_id
end
save() click to toggle source
# File lib/string_to_ipa.rb, line 41
def save
  attributes = []

  instance_variables.each do |i|
    attributes << i.to_s.delete("@")
  end

  query_hash = {}

  attributes.each do |a|
    value = self.send(a)
    query_hash[a] = value
  end

  query_hash.each do |key, value|
    database.execute("UPDATE phonetics SET #{key} = ? WHERE id = #{@id}", value)
  end
end
to_ipa() click to toggle source
# File lib/string_to_ipa.rb, line 16
def to_ipa
  phonetic = database.execute("SELECT phonetic from phonetics where word = ?", @word.upcase)

  if phonetic == []
    return @word
  else
    return phonetic[0]["phonetic"]
  end
end
to_word() click to toggle source
# File lib/string_to_ipa.rb, line 26
def to_word
  word = database.execute("SELECT word from phonetics where phonetic = ?", @phonetic)

  if word == []
    return @phonetic
  else
    return word[0]["word"].downcase
  end
end

Private Instance Methods

database() click to toggle source
# File lib/string_to_ipa.rb, line 71
def database
  @database ||= begin
    db = SQLite3::Database.new(File.join(File.expand_path(File.dirname(__FILE__)), "..", "ipagem.db"))
    db.results_as_hash = true
    db.execute( "PRAGMA encoding = \"UTF-16\"" )
    db
  end
end