Sha256: d69c7c1f70dedbe384e742b37e17d3553b2f82cb3248a0769f75aa9db6d2dd9e

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # GemSpec checks and compares gem version.
  class Rewriter::GemSpec
    OPERATORS = { eq: '==', lt: '<', gt: '>', lte: '<=', gte: '>=', ne: '!=' }.freeze

    # Initialize a gem_spec.
    #
    # @param name [String] gem name
    # @param comparator [Hash] comparator to gem version, e.g. {eq: '2.0.0'},
    #   comparator key can be eq, lt, gt, lte, gte or ne.
    def initialize(name, comparator)
      @name = name
      if Hash === comparator
        @operator = comparator.keys.first
        @version = Gem::Version.new comparator.values.first
      else
        @operator = :eq
        @version = Gem::Version.new comparator
      end
    end

    # Check if the specified gem version in Gemfile.lock matches gem_spec comparator.
    #
    # @return [Boolean] true if matches, otherwise false.
    # @raise [Synvert::Core::GemfileLockNotFound] raise if Gemfile.lock does not exist.
    def match?
      gemfile_lock_path = File.join(Configuration.path, 'Gemfile.lock')

      # if Gemfile.lock does not exist, just ignore this check
      return true unless File.exist?(gemfile_lock_path)

      parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
      if spec = parser.specs.find { |spec| spec.name == @name }
        Gem::Version.new(spec.version).send(OPERATORS[@operator], @version)
      else
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
synvert-core-0.20.0 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.19.0 lib/synvert/core/rewriter/gem_spec.rb