Class: SemanticRange::Comparator

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_range/comparator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comp, loose) ⇒ Comparator

Returns a new instance of Comparator.



4
5
6
7
8
9
10
11
# File 'lib/semantic_range/comparator.rb', line 4

def initialize(comp, loose)
  comp = comp.value if comp.is_a?(Comparator)
  @loose = loose

  parse(comp)

  @value = @semver == ANY ? '' : @operator + @semver.version
end

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



3
4
5
# File 'lib/semantic_range/comparator.rb', line 3

def operator
  @operator
end

#semverObject (readonly)

Returns the value of attribute semver.



3
4
5
# File 'lib/semantic_range/comparator.rb', line 3

def semver
  @semver
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/semantic_range/comparator.rb', line 3

def value
  @value
end

Instance Method Details

#intersects?(comp, loose: false, platform: nil) ⇒ Boolean Also known as: intersects

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/semantic_range/comparator.rb', line 33

def intersects?(comp, loose: false, platform: nil)
  comp = Comparator.new(comp, loose)

  if @operator == ''
    range_b = Range.new(comp.value, loose: loose, platform: platform)
    SemanticRange.satisfies?(@value, range_b, loose: loose, platform: platform)
  elsif comp.operator == ''
    range_a = Range.new(@value, loose: loose, platform: platform)
    SemanticRange.satisfies?(comp.semver, range_a, loose: loose, platform: platform)
  else
    same_direction_increasing      = (@operator == '>=' || @operator == '>') && (comp.operator == '>=' || comp.operator == '>')
    same_direction_decreasing      = (@operator == '<=' || @operator == '<') && (comp.operator == '<=' || comp.operator == '<')
    same_version                   = @semver.raw == comp.semver.raw
    different_directions_inclusive = (@operator == '>=' || @operator == '<=') && (comp.operator == '>=' || comp.operator == '<=')
    opposite_directions_lte        = SemanticRange.cmp(@semver, '<', comp.semver, loose: loose) &&
        ((@operator == '>=' || @operator == '>') && (comp.operator == '<=' || comp.operator == '<'))
    opposite_directions_gte        = SemanticRange.cmp(@semver, '>', comp.semver, loose: loose) &&
        ((@operator == '<=' || @operator == '<') && (comp.operator == '>=' || comp.operator == '>'))

    same_direction_increasing || same_direction_decreasing || (same_version && different_directions_inclusive) ||
        opposite_directions_lte || opposite_directions_gte
  end
end

#parse(comp) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
# File 'lib/semantic_range/comparator.rb', line 23

def parse(comp)
  m = comp.match(@loose ? COMPARATORLOOSE : COMPARATOR)
  raise InvalidComparator.new(comp) unless m

  @operator = m[1]
  @operator = '' if @operator == '='

  @semver = !m[2] ? ANY : Version.new(m[2], loose: @loose)
end

#satisfies_range?(range, loose: false, platform: nil) ⇒ Boolean Also known as: satisfies_range

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
# File 'lib/semantic_range/comparator.rb', line 57

def satisfies_range?(range, loose: false, platform: nil)
  range = Range.new(range, loose: loose, platform: platform)

  range.set.any? do |comparators|
    comparators.all? do |comparator|
      intersects?(comparator, loose: loose, platform: platform)
    end
  end
end

#test(version) ⇒ Object



17
18
19
20
21
# File 'lib/semantic_range/comparator.rb', line 17

def test(version)
  return true if @semver == ANY
  version = Version.new(version, @loose) if version.is_a?(String)
  SemanticRange.cmp(version, @operator, @semver, loose: @loose)
end

#to_sObject



13
14
15
# File 'lib/semantic_range/comparator.rb', line 13

def to_s
  @value
end