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
12
13
14
# File 'lib/semantic_range/comparator.rb', line 4

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

  @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

#parse(comp) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
# File 'lib/semantic_range/comparator.rb', line 26

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)
end

#test(version) ⇒ Object



20
21
22
23
24
# File 'lib/semantic_range/comparator.rb', line 20

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

#to_sObject



16
17
18
# File 'lib/semantic_range/comparator.rb', line 16

def to_s
  @value
end