Class: Vim::Flavor::VersionConstraint
- Inherits:
-
Object
- Object
- Vim::Flavor::VersionConstraint
- Defined in:
- lib/vim-flavor/versionconstraint.rb
Instance Attribute Summary collapse
-
#base_version ⇒ Object
readonly
Returns the value of attribute base_version.
-
#qualifier ⇒ Object
readonly
Specifies how to choose a suitable version according to base_version.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #compatible?(version) ⇒ Boolean
- #find_the_best_version(versions) ⇒ Object
-
#initialize(s) ⇒ VersionConstraint
constructor
A new instance of VersionConstraint.
- #to_s ⇒ Object
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_version ⇒ Object (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 |
#qualifier ⇒ Object (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
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vim-flavor/versionconstraint.rb', line 29 def self.parse(s) m = /^\s*(>=|~>|branch:)\s+(\S+)\s*$/.match(s) if m if m[1] == 'branch:' [Version.create(branch: m[2]), m[1]] else [Version.create(m[2]), m[1]] end else raise "Invalid version constraint: #{s.inspect}" end end |
Instance Method Details
#==(other) ⇒ Object
24 25 26 27 |
# File 'lib/vim-flavor/versionconstraint.rb', line 24 def ==(other) self.base_version == other.base_version && self.qualifier == other.qualifier end |
#compatible?(version) ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vim-flavor/versionconstraint.rb', line 42 def compatible?(version) if qualifier == '~>' PlainVersion === version and self.base_version.bump() > version and version >= self.base_version elsif qualifier == '>=' PlainVersion === version and version >= self.base_version elsif qualifier == 'branch:' BranchVersion === version and version.branch == self.base_version.branch else raise NotImplementedError end end |
#find_the_best_version(versions) ⇒ Object
58 59 60 61 62 |
# File 'lib/vim-flavor/versionconstraint.rb', line 58 def find_the_best_version(versions) versions. select {|v| compatible?(v)}. max() or raise 'There is no valid version' end |
#to_s ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/vim-flavor/versionconstraint.rb', line 13 def to_s() case when PlainVersion === base_version "#{qualifier} #{base_version}" when BranchVersion === base_version "#{qualifier} #{base_version.branch}" else throw "Unexpected base_version: #{base_version}" end end |