Sha256: fcf9918da51fada9a947951c5d6870f0ac02a44dfabd48ec5f42be52eee16fed

Contents?: true

Size: 890 Bytes

Versions: 2

Compression:

Stored size: 890 Bytes

Contents

# frozen_string_literal: true
require 'as_deprecation_tracker/whitelist_entry'
require 'rails/backtrace_cleaner'

module ASDeprecationTracker
  # Rewrites the whitelist configuration file to append new observed entries
  class Writer
    def initialize(filename)
      @filename = filename
      @contents = []
      @contents = YAML.load(File.read(filename)) || [] if File.exist?(filename)
    end

    def add(message, callstack)
      cleanup_match = WhitelistEntry::MESSAGE_CLEANUP_RE.match(message)
      message = cleanup_match[1] if cleanup_match

      callstack = Rails::BacktraceCleaner.new.clean(callstack, :silent)

      @contents << { 'message' => message, 'callstack' => callstack.first }
    end

    def contents
      @contents.sort_by { |e| e.values_at('message', 'callstack') }.to_yaml
    end

    def write_file
      File.write(@filename, contents)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
as_deprecation_tracker-1.1.0 lib/as_deprecation_tracker/writer.rb
as_deprecation_tracker-1.0.0 lib/as_deprecation_tracker/writer.rb