class YamlBackend

Constants

DB_FILE_PATH

Public Class Methods

new() click to toggle source
# File lib/pingilish/backends/yaml.rb, line 8
def initialize
  @db = YAML.load_file(DB_FILE_PATH)
end

Public Instance Methods

load() click to toggle source
# File lib/pingilish/backends/yaml.rb, line 12
def load
  @db
end
stats() click to toggle source
# File lib/pingilish/backends/yaml.rb, line 41
def stats
  
end
update(word_list) click to toggle source
# File lib/pingilish/backends/yaml.rb, line 16
def update(word_list)
  # opening yaml file. reading all items and appending new items
  filename = DB_FILE_PATH
  db = @db
  # TODO: check is it exist and the value is diffrenent then convert it to a hash
  db.merge!(word_list) if word_list.is_a?(Hash)
  
  begin
    # lock file and write to it
    if( File.exists? filename )
      file = File.new( filename, "r+")
    else
      file = File.new( filename, "w+" )
    end
    file.flock( File::LOCK_EX )
    file.truncate( 0 )
    file.rewind
    file.write( db.ya2yaml )
  ensure
    # unlock and close file
    file.flock( File::LOCK_UN )
    file.close
  end
end