Sha256: 53f88dcb4f604e170115779ad3e772ea2eaba204261fcbe7af0fc4ebaae8c4d2

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Avrolution
  module CompatibilityBreaksFile

    NONE = 'NONE'

    class DuplicateEntryError < StandardError
      def initialize(key)
        super("duplicate entry for key #{key}")
      end
    end

    def self.path
      Avrolution.compatibility_breaks_file
    end

    def self.add(name:,
                 fingerprint:,
                 with_compatibility: NONE,
                 after_compatibility: nil,
                 logger: Avrolution.logger)

      compatibility_break = Avrolution::CompatibilityBreak.new(
        name, fingerprint, with_compatibility, after_compatibility
      )
      compatibility_break.validate!

      compatibility_breaks = load
      raise DuplicateEntryError.new([name, fingerprint]) if compatibility_breaks.key?(compatibility_break.key)

      line = compatibility_break.line
      File.write(path, "#{line}\n", mode: 'a')
      logger.info("Added #{line.inspect} to #{path}")
    end

    def self.load
      return {} unless File.exist?(path)

      File.read(path).each_line.each_with_object({}) do |line, compatibility_breaks|
        next if line.blank? || /^#/ =~ line.strip

        compatibility_break = Avrolution::CompatibilityBreak.new(*line.strip.split(' '))
        compatibility_break.validate!

        raise DuplicateEntryError.new(compatibility_break.key) if compatibility_breaks.key?(compatibility_break.key)

        compatibility_breaks[compatibility_break.key] = compatibility_break
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
avrolution-0.9.0 lib/avrolution/compatibility_breaks_file.rb
avrolution-0.8.0 lib/avrolution/compatibility_breaks_file.rb
avrolution-0.7.2 lib/avrolution/compatibility_breaks_file.rb
avrolution-0.7.1 lib/avrolution/compatibility_breaks_file.rb
avrolution-0.7.0 lib/avrolution/compatibility_breaks_file.rb