Sha256: 70ce32aa0308238d7d323bdc518183bfda132dabc3bf9cf44c32f7067cef4daa

Contents?: true

Size: 994 Bytes

Versions: 8

Compression:

Stored size: 994 Bytes

Contents

# frozen_string_literal: true

module BundleUpdateInteractive
  class SemverChange
    SEVERITIES = %i[major minor patch].freeze

    def initialize(old_version, new_version)
      old_segments = old_version.to_s.split(".")
      new_segments = new_version.to_s.split(".")

      @same_segments = new_segments.take_while.with_index { |seg, i| seg == old_segments[i] }
      @diff_segments = new_segments[same_segments.length..]
    end

    def severity
      return nil if diff_segments.empty?

      SEVERITIES[same_segments.length] || :patch
    end

    SEVERITIES.each do |level|
      define_method(:"#{level}?") { severity == level }
    end

    def none?
      severity.nil?
    end

    def any?
      !!severity
    end

    def format
      parts = []
      parts << same_segments.join(".") if same_segments.any?
      parts << yield(diff_segments.join(".")) if diff_segments.any?
      parts.join(".")
    end

    private

    attr_reader :same_segments, :diff_segments
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bundle_update_interactive-0.5.0 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.4.0 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.3.0 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.2.1 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.2.0 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.1.2 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.1.1 lib/bundle_update_interactive/semver_change.rb
bundle_update_interactive-0.1.0 lib/bundle_update_interactive/semver_change.rb