Class: Datanorm::Documents::Preprocesses::Cache

Inherits:
Object
  • Object
show all
Includes:
Calls, Logging
Defined in:
lib/datanorm/documents/preprocesses/cache.rb

Overview

Writes a Datanorm record to disk for later retrieval.

Instance Method Summary collapse

Methods included from Logging

included

Instance Method Details

#callObject



17
18
19
20
21
22
# File 'lib/datanorm/documents/preprocesses/cache.rb', line 17

def call
  do_ensure_workdir
    .on_success { do_read_currently_cached_lines }
    .on_success { do_amend_content }
    .on_success { do_write_to_file }
end

#do_amend_contentObject



42
43
44
45
46
47
48
49
50
# File 'lib/datanorm/documents/preprocesses/cache.rb', line 42

def do_amend_content
  # Populate every line up until the wanted line.
  @lines[target_line_number - 1] ||= ''

  # Insert the content
  @lines[target_line_number - 1] = content

  Tron.success(:inserted_content)
end

#do_ensure_workdirObject



24
25
26
27
28
29
30
# File 'lib/datanorm/documents/preprocesses/cache.rb', line 24

def do_ensure_workdir
  return Tron.success :workdir_exists if workdir.directory?

  log { "Creating working dir `#{workdir}`" }
  FileUtils.mkdir_p(workdir)
  Tron.success(:workdir_created)
end

#do_read_currently_cached_linesObject



32
33
34
35
36
37
38
39
40
# File 'lib/datanorm/documents/preprocesses/cache.rb', line 32

def do_read_currently_cached_lines
  if filepath.exist?
    @lines = filepath.readlines(chomp: true)
    Tron.success(:loaded_current_file_content)
  else
    @lines = []
    Tron.success(:nothing_cached_yet)
  end
end

#do_write_to_fileObject



52
53
54
55
56
57
# File 'lib/datanorm/documents/preprocesses/cache.rb', line 52

def do_write_to_file
  log { "Writing line(s) at position #{target_line_number} to #{filepath}" }
  filepath.write @lines.join("\n")

  Tron.success :wrote_to_cache
end