Class: SafeDb::Eject

Inherits:
UseCase show all
Defined in:
lib/usecase/files/eject.rb

Overview

The eject use case writes (or overwrites) a file or files. Files are always ejected into the present working directory. If an overwrite is detected a backup is taken of the about to be clobbered file.

If a keyname is provided then only the file against that key is ejected. No keyname will eject every file in the opened chapter and verse.

Constant Summary

Constants inherited from UseCase

UseCase::APP_DIR_NAME, UseCase::COMMANDMENT, UseCase::ENV_VAR_KEY_NAME, UseCase::ENV_VAR_PREFIX_A, UseCase::ENV_VAR_PREFIX_B, UseCase::FILE_CONTENT_KEY, UseCase::FILE_KEY_PREFIX, UseCase::FILE_NAME_KEY

Instance Attribute Summary collapse

Attributes inherited from UseCase

#from_script

Instance Method Summary collapse

Methods inherited from UseCase

#check_post_conditions, #check_pre_conditions, #cleanup, #config_directory, #config_file, #flow_of_events, #get_master_database, #initialize, #post_validation, #pre_validation

Constructor Details

This class inherits a constructor from SafeDb::UseCase

Instance Attribute Details

#file_key=(value) ⇒ Object (writeonly)

Sets the attribute file_key

Parameters:

  • value

    the value to set the attribute file_key to.



14
15
16
# File 'lib/usecase/files/eject.rb', line 14

def file_key=(value)
  @file_key = value
end

#to_dir=(value) ⇒ Object (writeonly)

Sets the attribute to_dir

Parameters:

  • value

    the value to set the attribute to_dir to.



14
15
16
# File 'lib/usecase/files/eject.rb', line 14

def to_dir=(value)
  @to_dir = value
end

Instance Method Details

#executeObject

Files are always ejected into the present working directory and any about to be clobbered files are backed up with a timestamp.

If a keyname is provided then only the file against that key is ejected. No keyname will eject every file in the opened chapter and verse.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/usecase/files/eject.rb', line 21

def execute

  return unless ops_key_exists?
  master_db = get_master_database()
  return if unopened_envelope?( master_db )
  chapter_id = ENVELOPE_KEY_PREFIX + master_db[ ENV_PATH ]
  verse_id = master_db[ KEY_PATH ]
  chapter_data = KeyDb.from_json( KeyApi.content_unlock( master_db[ chapter_id ] ) )

  base64_content = chapter_data[ verse_id ][ "#{FILE_KEY_PREFIX}#{@file_key}" ][ FILE_CONTENT_KEY ]
  simple_filename = chapter_data[ verse_id ][ "#{FILE_KEY_PREFIX}#{@file_key}" ][ FILE_NAME_KEY ]

  # Do a mkdir_p if @to_dir has some valid non-whitespace text
  # If so check that we have permissions to write to the specified folder
  destination_dir = Dir.pwd if @to_dir.nil?
  destination_dir = @to_dir unless @to_dir.nil?

  file_full_path = File.join( destination_dir, simple_filename )
  backup_filename = KeyNow.yyjjj_hhmm_sst() + "-" + simple_filename
  backup_file_path = File.join( destination_dir, backup_filename )
  will_clobber = File.file?( file_full_path )

  puts ""
  puts "Clobbered File = #{backup_filename}" if will_clobber
  puts "Prescribed Directory = #{@to_dir}" unless @to_dir.nil?
  puts "Present Directory = #{Dir.pwd}" if @to_dir.nil?
  puts "Ejected Filename = #{simple_filename}"
  puts "The Full Filepath = #{file_full_path}"
  puts "Chapter and Verse = #{master_db[ENV_PATH]}::#{verse_id}"
  puts "Ejected File Key = #{@file_key}"
  puts ""
  puts "File successfully ejected from the safe."
  puts ""

  File.write( backup_file_path, File.read( file_full_path ) ) if will_clobber
  ::File.write( file_full_path, Base64.urlsafe_decode64( base64_content ) )

end