class Fuzz::Cache

Attributes

cache_file[R]
entries[R]

Public Class Methods

new(cache_file) click to toggle source
# File lib/fuzz/cache.rb, line 5
def initialize(cache_file)
  @cache_file = File.expand_path(cache_file)
  @entries = cache_entries(@cache_file)
end

Public Instance Methods

increment(title) click to toggle source
# File lib/fuzz/cache.rb, line 14
def increment(title)
  entries[title] = weight(title) + 1
  write
end
weight(title) click to toggle source
# File lib/fuzz/cache.rb, line 10
def weight(title)
  entries.fetch(title, 0)
end

Private Instance Methods

cache_entries(cache_file) click to toggle source
# File lib/fuzz/cache.rb, line 31
def cache_entries(cache_file)
  if File.exist?(cache_file)
    hash_from_file(File.new(cache_file))
  else
    {}
  end
end
directory() click to toggle source
# File lib/fuzz/cache.rb, line 47
def directory
  File.dirname(cache_file)
end
ensure_directory_exists() click to toggle source
# File lib/fuzz/cache.rb, line 51
def ensure_directory_exists
  FileUtils.mkdir_p(directory)
end
hash_from_file(cache_file) click to toggle source
# File lib/fuzz/cache.rb, line 39
def hash_from_file(cache_file)
  cache_file.readlines.inject({}) { |hash, line|
    count, title = line.split(" ", 2)
    hash[title.strip] = count.to_i
    hash
  }
end
write() click to toggle source
# File lib/fuzz/cache.rb, line 23
def write
  File.open(cache_file, "w") do |file|
    entries.each do |title, count|
      file.puts("#{ count } #{ title }")
    end
  end
end