Class: SafeDb::DeleteMe

Inherits:
UseCase show all
Defined in:
lib/usecase/edit/delete.rb

Overview

The delete use case delete’s one or more of the safe’s entities.

  • at verse level - it can delete one or more lines

  • at chapter level - it can delete one or more verses

  • at book level - it can delete one or more chapters

  • at safe level - it can delete one book

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

#entity_id=(value) ⇒ Object (writeonly)

Sets the attribute entity_id

Parameters:

  • value

    the value to set the attribute entity_id to.



14
15
16
# File 'lib/usecase/edit/delete.rb', line 14

def entity_id=(value)
  @entity_id = value
end

Instance Method Details

#executeObject

Deletion that currently expects an open chapter and verse and always wants to delete only one line (key/value pair).



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/usecase/edit/delete.rb', line 18

def execute

  return unless ops_key_exists?
  master_db = KeyApi.read_master_db()
  return if unopened_envelope?( master_db )

  chapter_id = ENVELOPE_KEY_PREFIX + master_db[ ENV_PATH ]
  chapter_exists = KeyApi.db_envelope_exists?( master_db[ chapter_id ] )
  chapter_data = KeyDb.from_json( KeyApi.content_unlock( master_db[ chapter_id ] ) ) if chapter_exists
  chapter_data = KeyDb.new() unless chapter_exists

  content_hdr = create_header()
  master_db[ chapter_id ] = {} unless chapter_exists
  verse_id = master_db[ KEY_PATH ]

  chapter_data.delete_entry( verse_id, @entity_id )
  chapter_data.delete_entry( verse_id, "#{FILE_KEY_PREFIX}#{@entity_id}" )

  KeyApi.content_lock( master_db[ chapter_id ], chapter_data.to_json, content_hdr )
  KeyApi.write_master_db( content_hdr, master_db )
  Show.new.flow_of_events

end