Class: StuffClassifier::FileStorage
- Inherits:
-
Object
- Object
- StuffClassifier::FileStorage
- Defined in:
- lib/stuff-classifier/storage.rb
Instance Method Summary collapse
- #_write_to_file ⇒ Object
-
#initialize(path) ⇒ FileStorage
constructor
A new instance of FileStorage.
- #load_state(classifier) ⇒ Object
- #purge_state(classifier) ⇒ Object
- #save_state(classifier) ⇒ Object
Constructor Details
#initialize(path) ⇒ FileStorage
Returns a new instance of FileStorage.
32 33 34 35 |
# File 'lib/stuff-classifier/storage.rb', line 32 def initialize(path) @storage = {} @path = path end |
Instance Method Details
#_write_to_file ⇒ Object
64 65 66 67 68 69 |
# File 'lib/stuff-classifier/storage.rb', line 64 def _write_to_file File.open(@path, 'w') do |fh| fh.flock(File::LOCK_EX) fh.write(@storage.to_msgpack) end end |
#load_state(classifier) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/stuff-classifier/storage.rb', line 37 def load_state(classifier) if @storage.length == 0 && File.exists?(@path) @storage = MessagePack.unpack(File.read(@path)) end if @storage.key? classifier.name _wcount, _ccount = @storage[classifier.name] classifier.instance_eval do @wcount = _wcount @ccount = _ccount end end end |
#purge_state(classifier) ⇒ Object
59 60 61 62 |
# File 'lib/stuff-classifier/storage.rb', line 59 def purge_state(classifier) @storage.delete(classifier.name) _write_to_file end |
#save_state(classifier) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/stuff-classifier/storage.rb', line 51 def save_state(classifier) name = classifier.name wcount = classifier.instance_variable_get :@wcount ccount = classifier.instance_variable_get :@ccount @storage[name] = [wcount, ccount] _write_to_file end |