Sha256: ced79a3c7931ea75a8cc8fe45de1fc297d792d21e491abc0f64e0bcf4d630f89
Contents?: true
Size: 998 Bytes
Versions: 19
Compression:
Stored size: 998 Bytes
Contents
# frozen_string_literal: true module RailsSpotlight class Storage attr_reader :key def initialize(key) @key = key end def write(value) FileUtils.mkdir_p dir_path # Use File.binwrite instead File.open(json_file, 'wb') { |file| file.write(value) } File.binwrite(json_file, value) maintain_file_pool(RailsSpotlight.config.storage_pool_size) end def read # avoid FileNotFound error File.exist?(json_file) ? File.read(json_file) : '[]' end private def maintain_file_pool(size) files = Dir["#{dir_path}/*.json"] files = files.sort_by { |f| -file_ctime(f) } (files[size..] || []).each do |file| FileUtils.rm_f(file) end end def file_ctime(file) File.stat(file).ctime.to_i rescue Errno::ENOENT 0 end def json_file File.join(dir_path, "#{key}.json") end def dir_path @dir_path ||= RailsSpotlight.config.storage_path end end end
Version data entries
19 entries across 19 versions & 1 rubygems