Class: Donjon::EncryptedFile

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

Instance Method Summary collapse

Constructor Details

#initialize(path:, actor:) ⇒ EncryptedFile

Returns a new instance of EncryptedFile.



8
9
10
11
# File 'lib/donjon/encrypted_file.rb', line 8

def initialize(path:, actor:)
  @path = path
  @actor = actor
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/donjon/encrypted_file.rb', line 13

def exist?
  _base_path.exist?
end

#readObject



21
22
23
24
25
26
27
# File 'lib/donjon/encrypted_file.rb', line 21

def read
  path = _path_for(@actor)
  exist? or raise 'file does not exist'
  path.exist? or raise 'you were not granted access'
  data = path.binread
  _decrypt_from(@actor, data)
end

#readable?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/donjon/encrypted_file.rb', line 17

def readable?
  _path_for(@actor).exist?
end

#write(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/donjon/encrypted_file.rb', line 29

def write(data)
  if data.nil?
    _base_path.rmtree if exist?
    return
  end

  User.each(@actor.repo) do |user|
    payload = _encrypt_for(user, data)
    path = _path_for(user)
    path.parent.mkpath
    path.binwrite(payload)
  end
end