Class: Subdb::UploadCache

Inherits:
Object
  • Object
show all
Defined in:
lib/subdb/upload_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ UploadCache

Returns a new instance of UploadCache.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/subdb/upload_cache.rb', line 31

def initialize(path)
  if File.exists? path
    data = open(path, "rb") { |io| io.read }

    begin
      @hash = Marshal.load(data)
    rescue TypeError => e
      @hash = {}
    end
  else
    @hash = {}
  end

  @path = path
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



24
25
26
# File 'lib/subdb/upload_cache.rb', line 24

def hash
  @hash
end

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/subdb/upload_cache.rb', line 24

def path
  @path
end

Class Method Details

.subtitle_hash(path) ⇒ Object



26
27
28
29
# File 'lib/subdb/upload_cache.rb', line 26

def self.subtitle_hash(path)
  data = open(path, "rb") { |io| io.read }
  Digest::SHA1.hexdigest(data)
end

Instance Method Details

#push(hash, path) ⇒ Object



52
53
54
55
56
57
# File 'lib/subdb/upload_cache.rb', line 52

def push(hash, path)
  return if uploaded?(hash, path)

  @hash[hash] ||= []
  @hash[hash].push(self.class.subtitle_hash(path))
end

#store!Object



63
64
65
66
67
# File 'lib/subdb/upload_cache.rb', line 63

def store!
  File.open(path, "wb") do |file|
    file << Marshal.dump(@hash)
  end
end

#uploaded?(hash, path) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/subdb/upload_cache.rb', line 47

def uploaded?(hash, path)
  list = @hash[hash] || []
  list.include? self.class.subtitle_hash(path)
end

#versions(hash) ⇒ Object



59
60
61
# File 'lib/subdb/upload_cache.rb', line 59

def versions(hash)
  (@hash[hash] || []).length
end