Class: BundleUpdateInteractive::Latest::GemRequirement

Inherits:
Object
  • Object
show all
Defined in:
lib/bundle_update_interactive/latest/gem_requirement.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts:, operator: nil, parsed_from: nil) ⇒ GemRequirement

Returns a new instance of GemRequirement.



17
18
19
20
21
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 17

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

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



15
16
17
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 15

def operator
  @operator
end

#partsObject (readonly)

Returns the value of attribute parts.



15
16
17
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 15

def parts
  @parts
end

Class Method Details

.parse(version) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 6

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

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

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

Instance Method Details

#==(other) ⇒ Object



51
52
53
54
55
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 51

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

  to_s == other.to_s
end

#exact?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 23

def exact?
  operator.nil?
end

#relaxObject



27
28
29
30
31
32
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 27

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

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

#shift(new_version) ⇒ Object

rubocop:disable Metrics/AbcSize



34
35
36
37
38
39
40
41
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 34

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



43
44
45
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 43

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

#to_sObject



47
48
49
# File 'lib/bundle_update_interactive/latest/gem_requirement.rb', line 47

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