Class: Vim::Flavor::VersionConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/vim-flavor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ VersionConstraint

Returns a new instance of VersionConstraint.



22
23
24
# File 'lib/vim-flavor.rb', line 22

def initialize(s)
  @base_version, @operator = parse(s)
end

Instance Attribute Details

#base_versionObject (readonly)

Returns the value of attribute base_version.



20
21
22
# File 'lib/vim-flavor.rb', line 20

def base_version
  @base_version
end

#operatorObject (readonly)

Returns the value of attribute operator.



20
21
22
# File 'lib/vim-flavor.rb', line 20

def operator
  @operator
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
# File 'lib/vim-flavor.rb', line 30

def ==(other)
  self.base_version == other.base_version &&
    self.operator == other.operator
end

#compatible?(other_version_or_s) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/vim-flavor.rb', line 44

def compatible?(other_version_or_s)
  v = Gem::Version.create(other_version_or_s)
  if @operator == '~>' then
    self.base_version.bump() > v and v >= self.base_version
  elsif @operator == '>=' then
    v >= self.base_version
  else
    raise NotImplementedError
  end
end

#find_the_best_version(versions) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/vim-flavor.rb', line 55

def find_the_best_version(versions)
  versions.
    select {|v| compatible?(v)}.
    sort().
    reverse().
    first
end

#parse(s) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/vim-flavor.rb', line 35

def parse(s)
  m = /^\s*(>=|~>)\s+(\S+)$/.match(s)
  if m then
    [Gem::Version.create(m[2]), m[1]]
  else
    raise "Invalid version constraint: #{s.inspect}"
  end
end

#to_sObject



26
27
28
# File 'lib/vim-flavor.rb', line 26

def to_s()
  "#{@operator} #{@base_version}"
end