Class: CemAcpt::Bolt::YamlFile

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_acpt/bolt/yaml_file.rb

Overview

Provides an abstraction for a YAML file Used as a base class for other YAML file abstractions

Direct Known Subclasses

Inventory, Project

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, #current_log_level, current_log_level, included, #logger, logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, #new_log_level, new_log_level, #new_logger, new_logger, verbose?, #verbose?

Constructor Details

#initialize(path) ⇒ YamlFile

Returns a new instance of YamlFile.



15
16
17
18
19
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 15

def initialize(path)
  @path = path
  @hash = {}
  @saved_hash = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 13

def path
  @path
end

Instance Method Details

#delete!Object



43
44
45
46
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 43

def delete!
  CemAcpt::Utils::Files.delete(path, log_prefix: self.class.to_s)
  @saved_hash = nil
end

#eq_to_disk?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 52

def eq_to_disk?
  return false unless File.exist?(path)

  disk_contents = CemAcpt::Utils::Files.read(path, log_prefix: self.class.to_s)
  return false unless disk_contents.is_a?(Hash)

  @hash.sort.to_h == disk_contents.sort.to_h
end

#gte_to_disk?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 61

def gte_to_disk?
  return false unless File.exist?(path)

  disk_contents = CemAcpt::Utils::Files.read(path, log_prefix: self.class.to_s)
  return false unless disk_contents.is_a?(Hash)

  @hash.sort.to_h >= disk_contents.sort.to_h
end

#latest_saved?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 48

def latest_saved?
  (@hash == @saved_hash) || eq_to_disk?
end

#load!Object



25
26
27
28
29
30
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 25

def load!
  logger.debug(self.class.to_s) { "Loading #{path}..." }
  @saved_hash = CemAcpt::Utils::Files.read(path, log_prefix: self.class.to_s)
  logger.verbose(self.class.to_s) { "Loaded #{path} contents: #{@saved_hash}" }
  @hash = @saved_hash.dup
end

#lte_to_disk?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 70

def lte_to_disk?
  return false unless File.exist?(path)

  disk_contents = CemAcpt::Utils::Files.read(path, log_prefix: self.class.to_s)
  return false unless disk_contents.is_a?(Hash)

  @hash.sort.to_h <= disk_contents.sort.to_h
end

#save!Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 32

def save!
  return if latest_saved?

  logger.debug(self.class.to_s) { 'Saving file...' }
  CemAcpt::Utils::Files.delete(path, log_prefix: self.class.to_s) if File.exist?(path)
  logger.debug(self.class.to_s) { "Creating #{path}..." }
  logger.verbose(self.class.to_s) { "New #{path} contents: #{@hash}" }
  CemAcpt::Utils::Files.write(path, @hash, log_prefix: self.class.to_s)
  @saved_hash = @hash
end

#to_hObject



21
22
23
# File 'lib/cem_acpt/bolt/yaml_file.rb', line 21

def to_h
  @hash
end