Class: YasstFile

Inherits:
File
  • Object
show all
Defined in:
lib/yasst/yasst_file.rb

Overview

Decorator pattern for File

TODO: initialise with a provider?

  • file encrypt state should be detected when passed through YasstFile.new - but need to add a provider to it for detection at init time

  • possible use of destructive/non-destructive methods for optionally writing files

  • check file open modes for destructive actions (e.g NOCREAT or something)

  • use a module/mixin for functional stuff

  • method for shredding files

  • verify support for blocks

  • helper methods and have a block that you can throw to open? (encrypt_and_shred, open_and_decrypt, read?)

Constant Summary collapse

DEFAULT_ENC_FILE_EXT =

if a provider does not provide a file_extension method then this is the default file extension that will be used

'enc'.freeze
CRYPTO_METHODS =
[:in_memory].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#encrypted=(value) ⇒ Object (writeonly)

Sets the attribute encrypted

Parameters:

  • value

    the value to set the attribute encrypted to.



16
17
18
# File 'lib/yasst/yasst_file.rb', line 16

def encrypted=(value)
  @encrypted = value
end

Instance Method Details

#decrypt(method = :in_memory) ⇒ Object

Decrypt self using a Yasst::Provider Returns a string with the path name of the decrypted file



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

def decrypt(method = :in_memory)
  raise Yasst::Error::AlreadyDecrypted,
        'File is not encrypted' unless encrypted?
  raise Yasst::Error::InvalidFileDestination,
        'decrypt_to is not set' if decrypt_to.nil?
  decrypt_using(method)
  decrypt_to
end

#decrypt_toObject



55
56
57
# File 'lib/yasst/yasst_file.rb', line 55

def decrypt_to
  @decrypt_to ||= default_decrypt_to
end

#decrypt_to=(d_path) ⇒ Object

Where to decrypt the file to



69
70
71
72
73
# File 'lib/yasst/yasst_file.rb', line 69

def decrypt_to=(d_path)
  raise Yasst::Error::InvalidFileDestination,
        'cannot overwrite self' if d_path == path
  @decrypt_to = d_path
end

#encrypt(method = :in_memory) ⇒ Object

Encrypt self using a Yasst::Provider Returns a string with the path name of the encrypted file



26
27
28
29
30
31
32
33
# File 'lib/yasst/yasst_file.rb', line 26

def encrypt(method = :in_memory)
  raise Yasst::Error::AlreadyEncrypted,
        'File is already encrypted' if encrypted?
  raise Yasst::Error::InvalidFileDestination,
        'encrypt_to is not set' if encrypt_to.nil?
  encrypt_using(method)
  encrypt_to
end

#encrypt_toObject



51
52
53
# File 'lib/yasst/yasst_file.rb', line 51

def encrypt_to
  @encrypt_to ||= default_encrypt_to
end

#encrypt_to=(e_path) ⇒ Object

Where to write the encrypted version of the file to



61
62
63
64
65
# File 'lib/yasst/yasst_file.rb', line 61

def encrypt_to=(e_path)
  raise Yasst::Error::InvalidFileDestination,
        'cannot overwrite self' if e_path == path
  @encrypt_to = e_path
end

#encrypted?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/yasst/yasst_file.rb', line 47

def encrypted?
  @encrypted ||= false
end

#matches_provider_extension?Boolean

If a file matches the provider profiles file extension, it is deemed to be encrypted by default Where a provider does not support the file_extension method, the default extension is used to match against.

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/yasst/yasst_file.rb', line 88

def matches_provider_extension?
  ext = DEFAULT_ENC_FILE_EXT
  (@provider.profile.respond_to? 'file_extension') &&
    ext = @provider.profile.file_extension
  path =~ /#{ext}$/
end

#provider=(provider) ⇒ Object

Setter method for provider. Also sets @encrypted where the filename matches the extension for the provider, unless it has previously been set



78
79
80
81
# File 'lib/yasst/yasst_file.rb', line 78

def provider=(provider)
  @provider = provider
  @encrypted.nil? && (matches_provider_extension? && @encrypted = true)
end