Class: Kozo::Backends::Local

Inherits:
Kozo::Backend show all
Defined in:
lib/kozo/backends/local.rb

Direct Known Subclasses

Git

Instance Attribute Summary collapse

Attributes inherited from Kozo::Backend

#configuration, #directory

Instance Method Summary collapse

Methods inherited from Kozo::Backend

#==, #state, #state=

Constructor Details

#initialize(configuration, directory) ⇒ Local

Returns a new instance of Local.



13
14
15
16
17
# File 'lib/kozo/backends/local.rb', line 13

def initialize(configuration, directory)
  super

  @directory ||= Kozo.options.directory
end

Instance Attribute Details

#backupsObject

Returns the value of attribute backups.



10
11
12
# File 'lib/kozo/backends/local.rb', line 10

def backups
  @backups
end

#fileObject



59
60
61
# File 'lib/kozo/backends/local.rb', line 59

def file
  @file ||= "kozo.kzstate"
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



11
12
13
# File 'lib/kozo/backends/local.rb', line 11

def fingerprint
  @fingerprint
end

Instance Method Details

#dataObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kozo/backends/local.rb', line 28

def data
  Kozo.logger.debug "Reading local state in #{path}"

  YAML
    .safe_load(File.read(path), permitted_classes: [Time, Date])
    .deep_symbolize_keys
rescue Errno::ENOENT, Errno::ENOTDIR
  raise StateError, "local state at #{path} not initialized"
rescue Psych::SyntaxError => e
  raise StateError, "could not read state file: #{e.message}"
end

#data=(value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kozo/backends/local.rb', line 40

def data=(value)
  Kozo.logger.debug "Writing local state in #{path}"

  return if @data.hash == value.hash

  @data = value

  return if Kozo.options.dry_run?

  # Write local state to temporary file
  File.write("#{path}.tmp", value.deep_stringify_keys.to_yaml)

  # Write backup state file
  FileUtils.mv(path, "#{path}.#{DateTime.current.to_i}.kzbackup") if backups && File.exist?(path)

  # Move temporary file to local state file
  FileUtils.mv("#{path}.tmp", path)
end

#initialize!Object



19
20
21
22
23
24
25
26
# File 'lib/kozo/backends/local.rb', line 19

def initialize!
  Kozo.logger.debug "Initializing local state in #{path}"

  return if File.exist?(path)

  # Initialize empty local state
  self.state = State.new
end

#pathObject



63
64
65
# File 'lib/kozo/backends/local.rb', line 63

def path
  @path ||= File.join(directory, file)
end