Sha256: 26a4da58f866c1f13be77d9a20d03d6d4260899ca0a7762647dcdf7e8def7a7d

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module Runbook::Util
  module Repo
    FILE_ID = "data"
    FILE_PERMISSIONS = 0600

    def self.load(metadata)
      title = metadata[:book_title]
      file = _file(title)
      if File.exist?(file)
        msg = "Repo file #{file} detected. Loading previous state..."
        metadata[:toolbox].output(msg)
        metadata[:repo] = ::YAML::load_file(file)
      end
    end

    def self.save(repo, book_title:)
      File.open(_file(book_title), 'w', FILE_PERMISSIONS) do |f|
        f.write(repo.to_yaml)
      end
    end

    def self.delete(title)
      FileUtils.rm_f(_file(title))
    end

    def self._file(book_title)
      "#{Dir.tmpdir}/runbook_#{FILE_ID}_#{ENV["USER"]}_#{_slug(book_title)}.yml"
    end

    def self._slug(title)
      title.titleize.gsub(/[\/\s]+/, "").underscore.dasherize
    end

    def self.register_save_repo_hook(base)
      base.register_hook(
        :save_repo_hook,
        :after,
        Object,
      ) do |object, metadata|
        repo = metadata[:repo]
        title = metadata[:book_title]
        Runbook::Util::Repo.save(repo, book_title: title)
      end
    end

    def self.register_delete_stored_repo_hook(base)
      base.register_hook(
        :delete_stored_repo_hook,
        :after,
        Runbook::Entities::Book,
      ) do |object, metadata|
        title = metadata[:book_title]
        Runbook::Util::Repo.delete(title)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
careacademy-runbook-1.2.0 lib/runbook/util/repo.rb
runbook-1.1.0 lib/runbook/util/repo.rb
runbook-1.0.0 lib/runbook/util/repo.rb