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.

RBS:

  • (path: String) -> void



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

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

Instance Attribute Details

#pathObject (readonly)

RBS:

  • @path: String



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

def path
  @path
end

Instance Method Details

#deleteObject

RBS:

  • () -> void



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

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

#exists?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

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

#readObject

RBS:

  • () -> String?



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

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

#write(data) ⇒ Object

RBS:

  • (String) -> Integer



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

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