Class: Compose::HandleExit

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wordword/interactors/compose/handle_exit.rb

Constant Summary collapse

MERGE =
:merge
REWRITE =
:rewrite

Instance Method Summary collapse

Constructor Details

#initialize(command_context) ⇒ HandleExit

Returns a new instance of HandleExit.



16
17
18
# File 'lib/wordword/interactors/compose/handle_exit.rb', line 16

def initialize(command_context)
  @command_context = command_context
end

Instance Method Details

#call(words) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wordword/interactors/compose/handle_exit.rb', line 20

def call(words)
  return unless words.any?
  return unless prompt.yes?("Save the file?")

  filename = prompt.ask("What is the filename?")
  if File.exist?(filename)
    file_mode = ask_file_mode
    if file_mode == REWRITE
      write_words(filename, words)
    elsif file_mode == MERGE
      existing_words = ReadWordTable.new.call(filename: filename).value!
      merged_words = existing_words.merge(words)

      write_words(filename, merged_words)
    end
  else
    write_words(filename, words)
  end
end