Class: Bundleup::VersionSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/bundleup/version_spec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts:, operator: nil) ⇒ VersionSpec

Returns a new instance of VersionSpec.



15
16
17
18
# File 'lib/bundleup/version_spec.rb', line 15

def initialize(parts:, operator: nil)
  @parts = parts
  @operator = operator
end

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



13
14
15
# File 'lib/bundleup/version_spec.rb', line 13

def operator
  @operator
end

#partsObject (readonly)

Returns the value of attribute parts.



13
14
15
# File 'lib/bundleup/version_spec.rb', line 13

def parts
  @parts
end

Class Method Details

.parse(version) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/bundleup/version_spec.rb', line 3

def self.parse(version)
  return version if version.is_a?(VersionSpec)

  version = version.strip
  _, operator, number = version.match(/^([^\d\s]*)\s*(.+)/).to_a
  operator = nil if operator.empty?

  new(parts: number.split("."), operator:)
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
51
52
# File 'lib/bundleup/version_spec.rb', line 48

def ==(other)
  return false unless other.is_a?(VersionSpec)

  to_s == other.to_s
end

#exact?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/bundleup/version_spec.rb', line 20

def exact?
  operator.nil?
end

#relaxObject



24
25
26
27
28
29
# File 'lib/bundleup/version_spec.rb', line 24

def relax
  return self if %w[!= > >=].include?(operator)
  return self.class.parse(">= 0") if %w[< <=].include?(operator)

  self.class.new(parts:, operator: ">=")
end

#shift(new_version) ⇒ Object

rubocop:disable Metrics/AbcSize



31
32
33
34
35
36
37
38
# File 'lib/bundleup/version_spec.rb', line 31

def shift(new_version) # rubocop:disable Metrics/AbcSize
  return self.class.parse(new_version) if exact?
  return self if Gem::Requirement.new(to_s).satisfied_by?(Gem::Version.new(new_version))
  return self.class.new(parts: self.class.parse(new_version).parts, operator: "<=") if %w[< <=].include?(operator)

  new_slice = self.class.parse(new_version).slice(parts.length)
  self.class.new(parts: new_slice.parts, operator: "~>")
end

#slice(amount) ⇒ Object



40
41
42
# File 'lib/bundleup/version_spec.rb', line 40

def slice(amount)
  self.class.new(parts: parts[0, amount], operator:)
end

#to_sObject



44
45
46
# File 'lib/bundleup/version_spec.rb', line 44

def to_s
  [operator, parts.join(".")].compact.join(" ")
end