Module: Crabfarm::Modes::Shared::SnapshotDecorator

Defined in:
lib/crabfarm/modes/shared/snapshot_decorator.rb

Defined Under Namespace

Modules: Colors

Class Method Summary collapse

Class Method Details

.decorate(_reducer) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/crabfarm/modes/shared/snapshot_decorator.rb', line 13

def self.decorate(_reducer)
  loop do
    name = Ask.input "-- Name for #{_reducer.class.to_s} snapshot (blank to skip)".color Colors::QUESTION
    if name.empty?
      puts "-- Skipping snapshot".color Colors::WARNING
      break
    else
      file_path = _reducer.class.snapshot_path name

      if File.exist? file_path
        puts "-- Could not save snapshot, file already exist!".color Colors::ERROR
      else
        dir_path = file_path.split(File::SEPARATOR)[0...-1]
        FileUtils.mkpath dir_path.join(File::SEPARATOR) if dir_path.length > 0
        File.write file_path, _reducer.raw_document
        puts "-- Snapshot written to #{file_path}".color Colors::RESULT
        break
      end
    end
  end

  nil
end