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.

Constant Summary collapse

OPERATORS =
{eq: '==', lt: '<', gt: '>', lte: '<=', gte: '>=', ne: '!='}

Instance Method Summary collapse

Constructor Details

#initialize(name, comparator) ⇒ GemSpec

Initialize a gem_spec.

Parameters:

  • name (String)

    gem name

  • comparator (Hash)

    comparator to gem version, e.g. ‘2.0.0’, comparator key can be eq, lt, gt, lte, gte or ne.



13
14
15
16
17
18
19
20
21
22
# File 'lib/synvert/core/rewriter/gem_spec.rb', line 13

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

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:



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

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