Class: YamlBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/pingilish/backends/yaml.rb

Constant Summary collapse

DB_FILE_PATH =
File.join(File.dirname(__FILE__), '../../../db/pingilish_words.yml')

Instance Method Summary collapse

Constructor Details

#initializeYamlBackend

Returns a new instance of YamlBackend.



8
9
10
# File 'lib/pingilish/backends/yaml.rb', line 8

def initialize
  @db = YAML.load_file(DB_FILE_PATH)
end

Instance Method Details

#loadObject



12
13
14
# File 'lib/pingilish/backends/yaml.rb', line 12

def load
  @db
end

#statsObject



41
42
43
# File 'lib/pingilish/backends/yaml.rb', line 41

def stats
  
end

#update(word_list) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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