class SimpleStatsStore::FileDump

Public Class Methods

new(dir, options = {}) click to toggle source
# File lib/simple_stats_store/file_dump.rb, line 5
def initialize(dir, options = {})
  @dir = dir
  @max = options.has_key?(:max) ? options[:max] : nil
end

Public Instance Methods

each(&block) click to toggle source
# File lib/simple_stats_store/file_dump.rb, line 26
def each(&block)
  files_contents.each &block
end
files_contents() click to toggle source
# File lib/simple_stats_store/file_dump.rb, line 10
def files_contents
  contents = []
  Dir["#{@dir}/**/*.stats"].each do |f|
    begin
      data = File.open(f, 'r').read
      if /\n---\n$/.match(data)
        contents << data
        File.delete(f)
      end
    rescue Errno::ENOENT
      puts "Failed to open file #{f}"
    end
  end
  contents
end
write(model, data) click to toggle source
# File lib/simple_stats_store/file_dump.rb, line 30
def write(model, data)
  i = 0
  subdir = File.expand_path(model.to_s, @dir)
  Dir.mkdir(subdir) if ! Dir.exists?(subdir)
  if ! @max.nil?
    files = Dir["#{subdir}/*"].sort { |a, b| File.ctime(a) <=> File.ctime(b) }
    if files.length >= @max
      files[0..(@max-files.length)].each do |f|
        File.delete(f)
      end
    end
  end

  while File.exists?(File.expand_path("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", subdir))
    i += 1
  end
  File.open(File.expand_path("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", subdir), 'w') do |f|
    f.puts "---"
    f.puts model.to_s
    data.each do |key, value|
      f.puts "#{key.to_s}: #{value}"
    end
    f.puts "---"
  end
end