Class: SafeDb::Write

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

Overview

The write use case writes (or overwrites) a file at the out url destination.

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_url=(value) ⇒ Object (writeonly)

Sets the attribute file_url

Parameters:

  • value

    the value to set the attribute file_url to.



9
10
11
# File 'lib/usecase/files/write.rb', line 9

def file_url=(value)
  @file_url = value
end

Instance Method Details

#executeObject

The read use case pulls a file in from either an accessible filesystem or from a remote http, https, git, S3, GoogleDrive and/or ssh source.



13
14
15
16
17
18
19
20
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
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/usecase/files/write.rb', line 13

def execute

  return unless ops_key_exists?
  master_db = get_master_database()
  return if unopened_envelope?( master_db )

  # Get the open chapter identifier (id).
  # Decide whether chapter already exists.
  # Then get (or instantiate) the chapter's hash data structure
  chapter_id = ENVELOPE_KEY_PREFIX + master_db[ ENV_PATH ]
  verse_id = master_db[ KEY_PATH ]
  chapter_exists = KeyApi.db_envelope_exists?( master_db[ chapter_id ] )


  # @todo begin
  # Throw an exception (error) if the chapter
  # either exists and is empty or does not exist.
  # @todo end


  # Unlock the chapter data structure by supplying
  # key/value mini-dictionary breadcrumbs sitting
  # within the master database at the section labelled
  # envelope@<<actual_chapter_id>>.
  chapter_data = KeyDb.from_json( KeyApi.content_unlock( master_db[ chapter_id ] ) )


  # Unlock the file content by supplying the
  # key/value mini-dictionary breadcrumbs sitting
  # within the chapter's data structure in the
  # section labelled <<verse_id>>.
  file_content = KeyApi.content_unlock( chapter_data[ verse_id ] )


  # We read the location url we plan to eject the
  # file out into.
  file_path = @file_url ? @file_url : chapter_data[ verse_id ][ "@out.url" ]
  file_name = ::File.basename( file_path)

  # If the directory the file will be exported to does
  # not exist we promptly create it.
  FileUtils.mkdir_p( File.dirname( file_path ) )

  # Create a backup file if we can detect that a
  # file occupies the eject (write) filepath.
  backup_file_path = ::File.join( ::File.dirname( file_path ), KeyNow.yyjjj_hhmm_sst() + "-" + file_name )
  ::File.write( backup_file_path, ::File.read( file_path ) ) if ::File.file?( file_path )


  # Now write (and if necessary overwrite) the eject
  # file url path with the previously ingested content.
  ::File.write( file_path, file_content )


  # Communicate that the indicated file has just been
  # successfully written out from the safe.
  print_file_success( master_db[ ENV_PATH ], verse_id, file_path )

end