Class: Vim::Flavor::VersionConstraint

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ VersionConstraint

Returns a new instance of VersionConstraint.



9
10
11
# File 'lib/vim-flavor/versionconstraint.rb', line 9

def initialize(s)
  @base_version, @qualifier = self.class.parse(s)
end

Instance Attribute Details

#base_versionObject (readonly)

Returns the value of attribute base_version.



4
5
6
# File 'lib/vim-flavor/versionconstraint.rb', line 4

def base_version
  @base_version
end

#qualifierObject (readonly)

Specifies how to choose a suitable version according to base_version.



7
8
9
# File 'lib/vim-flavor/versionconstraint.rb', line 7

def qualifier
  @qualifier
end

Class Method Details

.parse(s) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/vim-flavor/versionconstraint.rb', line 22

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

Instance Method Details

#==(other) ⇒ Object



17
18
19
20
# File 'lib/vim-flavor/versionconstraint.rb', line 17

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

#compatible?(other_version_or_s) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/vim-flavor/versionconstraint.rb', line 31

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

#find_the_best_version(versions) ⇒ Object



42
43
44
45
46
# File 'lib/vim-flavor/versionconstraint.rb', line 42

def find_the_best_version(versions)
  versions.
    select {|v| compatible?(v)}.
    max()
end

#to_sObject



13
14
15
# File 'lib/vim-flavor/versionconstraint.rb', line 13

def to_s()
  "#{qualifier} #{base_version}"
end