class Cue::Store::File

Public Class Methods

new(root_path=nil) click to toggle source
# File lib/cue/store/file.rb, line 10
def initialize(root_path=nil)
  @root_path = root_path
  FileUtils.mkdir_p(items_path)
end

Public Instance Methods

clear() click to toggle source
# File lib/cue/store/file.rb, line 15
def clear
  keys.each { |key| delete(key) }
end
delete(key) click to toggle source
# File lib/cue/store/file.rb, line 19
def delete(key)
  FileUtils.rm_r(item_path(key))
  key_dir = dir_for(key)
  FileUtils.rm_r(key_dir) if Dir[::File.join(key_dir, '*')].empty?
end
each() { |items| ... } click to toggle source
# File lib/cue/store/file.rb, line 25
def each
  items = keys.map(&method(:read)).sort
  yield(items)
end
keys() click to toggle source
# File lib/cue/store/file.rb, line 30
def keys
  keys = []
  
  Dir.glob(::File.join(items_path, '*')).each do |prefix_dir|
    prefix = ::File.basename(prefix_dir)
    Dir.glob(::File.join(prefix_dir, '*')).each do |suffix_file|
      suffix = ::File.basename(suffix_file)
      keys << (prefix + suffix)
    end
  end
  
  keys
end
read(key) click to toggle source
# File lib/cue/store/file.rb, line 44
def read(key)
  return nil unless ::File.exists?(item_path(key))
  
  data = nil
  file_for_reading(item_path(key)) do |file|
    data = uncompress(file.read)
  end
  return data if data.nil?
  
  deserialize(data)
end
write(key, item) click to toggle source
# File lib/cue/store/file.rb, line 56
def write(key, item)
  FileUtils.mkdir_p(dir_for(key))
  file_for_writing(item_path(key)) do |file|
    data = compress(serialize(item))
    file.write(data)
  end
end

Private Instance Methods

compress(data) click to toggle source
# File lib/cue/store/file.rb, line 70
def compress(data)
  Zlib::Deflate.deflate(data)
end
deserialize(data) click to toggle source
# File lib/cue/store/file.rb, line 74
def deserialize(data)
  created_at, state, content = data.unpack('Z*Z*Z*')
  Cue::Item.new(content, created_at: created_at, state: state.to_sym)
end
dir_for(key) click to toggle source
# File lib/cue/store/file.rb, line 79
def dir_for(key)
  ::File.join(items_path, prefix(key))
end
file_for_reading(path) { |file| ... } click to toggle source
# File lib/cue/store/file.rb, line 87
def file_for_reading(path, &block)
  ::File.open(path, mode_for_reading) { |file| yield(file) }
end
file_for_writing(path) { |file| ... } click to toggle source
# File lib/cue/store/file.rb, line 83
def file_for_writing(path, &block)
  ::File.open(path, mode_for_writing) { |file| yield(file) }
end
item_path(key) click to toggle source
# File lib/cue/store/file.rb, line 91
def item_path(key)
  ::File.join(dir_for(key), suffix(key))
end
items_path() click to toggle source
# File lib/cue/store/file.rb, line 95
def items_path
  @items_path ||= ::File.join(root_path, 'items')
end
keys_path() click to toggle source
# File lib/cue/store/file.rb, line 99
def keys_path
  @keys_path ||= ::File.join(root_path, 'keys')
end
mode_for_reading() click to toggle source
# File lib/cue/store/file.rb, line 103
def mode_for_reading
  ::File::RDONLY
end
mode_for_writing() click to toggle source
# File lib/cue/store/file.rb, line 107
def mode_for_writing
  ::File::CREAT | ::File::WRONLY | ::File::TRUNC
end
prefix(key) click to toggle source
# File lib/cue/store/file.rb, line 111
def prefix(key)
  key[0..1]
end
root_path() click to toggle source
# File lib/cue/store/file.rb, line 66
def root_path
  @root_path ||= ::File.join(ENV['HOME'], '.cue')
end
serialize(item) click to toggle source
# File lib/cue/store/file.rb, line 115
def serialize(item)
  data = [item.created_at.to_i.to_s, item.state.to_s, item.content.to_s]
  data.pack('Z*Z*Z*')
end
suffix(key) click to toggle source
# File lib/cue/store/file.rb, line 120
def suffix(key)
  key[2...(key.size)]
end
uncompress(data) click to toggle source
# File lib/cue/store/file.rb, line 124
def uncompress(data)
  Zlib::Inflate.inflate(data)
end