Sha256: 077c42c35f880540ab08156f3bef00692bac7b85510868f1d61c2cec4614ada0

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module Runbook::Util
  module StoredPose
    FILE_ID = "current_position"
    FILE_PERMISSIONS = 0600

    def self.load(metadata)
      title = metadata[:book_title]
      file = _file(title)
      if File.exist?(file)
        ::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_pose_hook(base)
      base.register_hook(
        :save_pose_hook,
        :before,
        Object,
      ) do |object, metadata|
        position = metadata[:position]
        title = metadata[:book_title]
        Runbook::Util::StoredPose.save(position, book_title: title)
      end
    end

    def self.register_delete_stored_pose_hook(base)
      base.register_hook(
        :delete_stored_pose_hook,
        :after,
        Runbook::Entities::Book,
      ) do |object, metadata|
        title = metadata[:book_title]
        Runbook::Util::StoredPose.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/stored_pose.rb
runbook-1.1.0 lib/runbook/util/stored_pose.rb
runbook-1.0.0 lib/runbook/util/stored_pose.rb