Class: StuffClassifier::FileStorage

Inherits:
Storage
  • Object
show all
Defined in:
lib/stuff-classifier/storage.rb

Instance Attribute Summary

Attributes inherited from Storage

#storage

Instance Method Summary collapse

Methods inherited from Storage

#classifier_to_storage, #clear_storage, #storage_to_classifier

Constructor Details

#initialize(path) ⇒ FileStorage

Returns a new instance of FileStorage.



59
60
61
62
# File 'lib/stuff-classifier/storage.rb', line 59

def initialize(path)
  super
  @path = path
end

Instance Method Details

#_write_to_fileObject



82
83
84
85
86
87
# File 'lib/stuff-classifier/storage.rb', line 82

def _write_to_file
  File.open(@path, 'wb') do |fh|
    fh.flock(File::LOCK_EX)
    fh.write(Marshal.dump(@storage))
  end
end

#load_state(classifier) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/stuff-classifier/storage.rb', line 64

def load_state(classifier)
  if @storage.length == 0 && File.exists?(@path)
    data = File.open(@path, 'rb') { |f| f.read }
    @storage = Marshal.load(data)
  end
  storage_to_classifier(classifier)
end

#purge_state(classifier) ⇒ Object



77
78
79
80
# File 'lib/stuff-classifier/storage.rb', line 77

def purge_state(classifier)
  clear_storage(classifier)
  _write_to_file
end

#save_state(classifier) ⇒ Object



72
73
74
75
# File 'lib/stuff-classifier/storage.rb', line 72

def save_state(classifier)
  classifier_to_storage(classifier)
  _write_to_file
end