lib/zold/copies.rb in zold-0.14.37 vs lib/zold/copies.rb in zold-0.14.38
- old
+ new
@@ -32,10 +32,13 @@
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
module Zold
# All copies
class Copies
+ # Extension for copy files
+ EXT = '.zc'
+
def initialize(dir, log: Log::Quiet.new)
raise 'Dir can\'t be nil' if dir.nil?
@dir = dir
raise 'Log can\'t be nil' if log.nil?
@log = log
@@ -55,11 +58,11 @@
list = load
list.reject! { |s| s[:time] < Time.now - 24 * 60 * 60 }
save(list)
deleted = 0
files.each do |f|
- next unless list.find { |s| s[:name] == File.basename(f, Wallet::EXTENSION) }.nil?
+ next unless list.find { |s| s[:name] == File.basename(f, Copies::EXT) }.nil?
file = File.join(@dir, f)
size = File.size(file)
File.delete(file)
@log.debug("Copy at #{f} deleted: #{size}b")
deleted += 1
@@ -97,21 +100,21 @@
raise "Score can't be negative: #{score}" if score.negative?
@mutex.synchronize do
FileUtils.mkdir_p(@dir)
list = load
target = list.find do |s|
- f = File.join(@dir, "#{s[:name]}#{Wallet::EXTENSION}")
+ f = File.join(@dir, "#{s[:name]}#{Copies::EXT}")
File.exist?(f) && AtomicFile.new(f).read == content
end
if target.nil?
max = Dir.new(@dir)
- .select { |f| File.basename(f, Wallet::EXTENSION) =~ /^[0-9]+$/ }
+ .select { |f| File.basename(f, Copies::EXT) =~ /^[0-9]+$/ }
.map(&:to_i)
.max
max = 0 if max.nil?
name = (max + 1).to_s
- AtomicFile.new(File.join(@dir, "#{name}#{Wallet::EXTENSION}")).write(content)
+ AtomicFile.new(File.join(@dir, "#{name}#{Copies::EXT}")).write(content)
else
name = target[:name]
end
list.reject! { |s| s[:host] == host && s[:port] == port }
list << {
@@ -129,11 +132,11 @@
def all
@mutex.synchronize do
load.group_by { |s| s[:name] }.map do |name, scores|
{
name: name,
- path: File.join(@dir, "#{name}#{Wallet::EXTENSION}"),
+ path: File.join(@dir, "#{name}#{Copies::EXT}"),
score: scores.select { |s| s[:time] > Time.now - 24 * 60 * 60 }
.map { |s| s[:score] }
.inject(&:+) || 0
}
end.select { |c| File.exist?(c[:path]) }.sort_by { |c| c[:score] }.reverse
@@ -167,13 +170,13 @@
end.join("\n")
)
end
def files
- Dir.new(@dir).select { |f| File.basename(f, Wallet::EXTENSION) =~ /^[0-9]+$/ }
+ Dir.new(@dir).select { |f| File.basename(f, Copies::EXT) =~ /^[0-9]+$/ }
end
def file
- File.join(@dir, "scores#{Wallet::EXTENSION}")
+ File.join(@dir, "scores#{Copies::EXT}")
end
end
end