module LetsCert::FileIOPluginMixin

Mixin for IOPmugin subclasses that handle files @author Sylvain Daubert

Public Instance Methods

load() click to toggle source

Load data from file named #name @return [Hash]

# File lib/letscert/io_plugins/file_io_plugin_mixin.rb, line 30
def load
  logger.debug { "Loading #{@name}" }

  begin
    content = File.read(@name)
  rescue Errno::ENOENT
    logger.info { "no #{@name} file" }
    return self.class.empty_data
  end

  load_from_content(content)
end
load_from_content(_content) click to toggle source

@abstract @param [String] _content @return [Hash]

# File lib/letscert/io_plugins/file_io_plugin_mixin.rb, line 46
def load_from_content(_content)
  raise NotImplementedError
end
save_to_file(data) click to toggle source

Save data to file #name @param [Hash] data @return [void] @raise [Error] IO error

# File lib/letscert/io_plugins/file_io_plugin_mixin.rb, line 54
def save_to_file(data)
  return if data.nil?

  # Return if content did not change
  if File.exist? name
    old_content = File.read(name)
    return if old_content == data
  end

  logger.info { "saving #{@name}" }
  begin
    File.open(name, 'w') do |f|
      f.write(data)
    end
  rescue Errno => ex
    @logger.error { ex.message }
    raise Error, "Error when saving #{@name}"
  end
end