Class: Ruco::FileStore

Inherits:
Object show all
Defined in:
lib/ruco/file_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(folder, options = {}) ⇒ FileStore

Returns a new instance of FileStore.



7
8
9
10
# File 'lib/ruco/file_store.rb', line 7

def initialize(folder, options={})
  @folder = File.expand_path(folder)
  @options = options
end

Instance Method Details

#cache(key, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/ruco/file_store.rb', line 23

def cache(key, &block)
  value = get(key)
  if value.nil?
    value = yield
    set(key, value)
  end
  value
end

#clearObject



37
38
39
# File 'lib/ruco/file_store.rb', line 37

def clear
  FileUtils.rm_rf(@folder)
end

#delete(key) ⇒ Object



32
33
34
35
# File 'lib/ruco/file_store.rb', line 32

def delete(key)
  FileUtils.rm(file(key))
rescue Errno::ENOENT
end

#file(key) ⇒ Object



41
42
43
# File 'lib/ruco/file_store.rb', line 41

def file(key)
  "#{@folder}/#{Digest::MD5.hexdigest(key)}.yml"
end

#get(key) ⇒ Object



18
19
20
21
# File 'lib/ruco/file_store.rb', line 18

def get(key)
  file = file(key)
  deserialize File.binary_read(file) if File.exist?(file)
end

#set(key, value) ⇒ Object



12
13
14
15
16
# File 'lib/ruco/file_store.rb', line 12

def set(key, value)
  FileUtils.mkdir_p @folder unless File.exist? @folder
  File.write(file(key), serialize(value))
  cleanup if @options[:keep]
end