Class: Synvert::Core::Rewriter::GemSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/core/rewriter/gem_spec.rb

Overview

GemSpec checks and compares gem version.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version) ⇒ GemSpec

Initialize a GemSpec.

Parameters:

  • name (String)

    gem name

  • version (String)

    gem version, e.g. '~> 2.0.0'



18
19
20
21
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 18

def initialize(name, version)
  @name = name
  @version = version
end

Instance Attribute Details

#nameString (readonly)

Returns the name of gem_spec.

Returns:

  • (String)

    the name of gem_spec



12
13
14
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 12

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



12
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 12

attr_reader :name, :version

Instance Method Details

#match?Boolean

Check if the specified gem version in Gemfile.lock matches gem_spec comparator.

Returns:

  • (Boolean)

    true if matches, otherwise false.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 26

def match?
  return true unless Configuration.strict

  gemfile_lock_path = File.expand_path(File.join(Configuration.root_path, 'Gemfile.lock'))

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

  ENV['BUNDLE_GEMFILE'] = Configuration.root_path # make sure bundler reads Gemfile.lock in the correct path
  parser = Bundler::LockfileParser.new(File.read(gemfile_lock_path))
  parser.specs.any? { |spec| Gem::Dependency.new(@name, @version).match?(spec) }
end