Sha256: 79de2833e222ff906c91cc6ad091489c190f94f1a7bf7ff73d9160f370571395

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/string/inflections"
require "bundler"
require "yaml"

module DeprecationToolkit
  module ReadWriteHelper
    def read(test)
      deprecation_file = recorded_deprecations_path(test)
      YAML.load(deprecation_file.read).fetch(test.name, [])
    rescue Errno::ENOENT
      []
    end

    def write(test, deprecations)
      deprecation_file = recorded_deprecations_path(test)
      create_deprecation_file(deprecation_file) unless deprecation_file.exist?

      content = YAML.load_file(deprecation_file)
      if deprecations.any?
        content[test.name] = deprecations
      else
        content.delete(test.name)
      end

      if content.any?
        deprecation_file.write(YAML.dump(content))
      else
        deprecation_file.delete
      end
    end

    private

    def create_deprecation_file(deprecation_file)
      deprecation_file.dirname.mkpath
      deprecation_file.write(YAML.dump({}))
    end

    def recorded_deprecations_path(test)
      deprecation_folder = if Configuration.deprecation_path.is_a?(Proc)
        Configuration.deprecation_path.call(test_location(test))
      else
        Configuration.deprecation_path
      end

      Bundler.root.join(deprecation_folder, "#{test.class.name.underscore}.yml")
    end

    def test_location(test)
      test.method(test.name).source_location[0]
    rescue NameError
      "unknown"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
deprecation_toolkit-1.2.2 lib/deprecation_toolkit/read_write_helper.rb
deprecation_toolkit-1.2.1 lib/deprecation_toolkit/read_write_helper.rb
deprecation_toolkit-1.2.0 lib/deprecation_toolkit/read_write_helper.rb
deprecation_toolkit-1.1.0 lib/deprecation_toolkit/read_write_helper.rb
deprecation_toolkit-1.0.3 lib/deprecation_toolkit/read_write_helper.rb
deprecation_toolkit-1.0.2 lib/deprecation_toolkit/read_write_helper.rb