Sha256: 0ca27f7fe6ecbe4e2e1453b95a7d9167c71f95d5ac0742b9d45182f2d381dddf

Contents?: true

Size: 1.4 KB

Versions: 27

Compression:

Stored size: 1.4 KB

Contents

# encoding: utf-8

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

    # Initialize a gem_spec.
    #
    # @param name [String] gem name
    # @param comparator [Hash] comparator to gem version, e.g. {eg: '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.instance.get(:path), 'Gemfile.lock')
      if File.exists? 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
      else
        raise GemfileLockNotFound.new 'Gemfile.lock does not exist'
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
synvert-core-0.9.1 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.9.0 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.8.2 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.8.1 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.8.0 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.7.5 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.7.4 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.7.3 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.7.2 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.7.1 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.6.5 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.6.4 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.6.3 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.6.2 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.6.0 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.5.3 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.5.2 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.5.1 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.5.0 lib/synvert/core/rewriter/gem_spec.rb
synvert-core-0.4.3 lib/synvert/core/rewriter/gem_spec.rb