Class: Archive

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder) ⇒ Archive

Returns a new instance of Archive.



6
7
8
# File 'lib/Archive.rb', line 6

def initialize folder
  @folder = folder
end

Class Method Details

.file_key(filename) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/Archive.rb', line 10

def self.file_key filename
  file_h = Digest::SHA2.new
  File.open(filename, 'r') do |fh|
    while buffer = fh.read(1024)
      file_h << buffer
    end
  end
  file_h
end

.hash_key(hash) ⇒ Object



20
21
22
# File 'lib/Archive.rb', line 20

def self.hash_key hash
  Digest::SHA2.new << hash.to_yaml
end

Instance Method Details

#get(params, file) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/Archive.rb', line 38

def get params, file
  key = get_key params
  if File.exists? @folder+'/'+key.to_s
    File.copy @folder+'/'+key.to_s, file
  else
    raise 'No such file'
  end
end

#get_key(params) ⇒ Object



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

def get_key params
  self.class.hash_key params
end

#has?(params) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/Archive.rb', line 28

def has? params
  key  = get_key params
  File.exists? @folder+'/'+key.to_s
end

#put(params, file) ⇒ Object



33
34
35
36
# File 'lib/Archive.rb', line 33

def put params, file
  key = get_key params
  File.copy @folder+'/'+file, key.to_s
end