Class: Classifier::Storage::File

Inherits:
Base show all
Defined in:
lib/classifier/storage/file.rb

Overview

File-based storage backend.

Example:

bayes = Classifier::Bayes.new('Spam', 'Ham')
bayes.storage = Classifier::Storage::File.new(path: "/var/models/spam.json")
bayes.train_spam("Buy now!")
bayes.save

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ File

Returns a new instance of File.



25
26
27
28
# File 'lib/classifier/storage/file.rb', line 25

def initialize(path:)
  super()
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/classifier/storage/file.rb', line 22

def path
  @path
end

Instance Method Details

#deleteObject



41
42
43
# File 'lib/classifier/storage/file.rb', line 41

def delete
  ::File.delete(@path) if exists?
end

#exists?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/classifier/storage/file.rb', line 46

def exists?
  ::File.exist?(@path)
end

#readObject



36
37
38
# File 'lib/classifier/storage/file.rb', line 36

def read
  exists? ? ::File.read(@path) : nil
end

#write(data) ⇒ Object



31
32
33
# File 'lib/classifier/storage/file.rb', line 31

def write(data)
  ::File.write(@path, data)
end