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 gem_spec.

Parameters:

  • name (String)

    gem name

  • version (String)

    gem version, e.g. ‘~> 2.0.0’,



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 6

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 6

def version
  @version
end

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.

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 21

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)

  ENV['BUNDLE_GEMFILE'] = Configuration.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