Class: Vim::Flavor::VersionConstraint
- Inherits:
-
Object
- Object
- Vim::Flavor::VersionConstraint
- Defined in:
- lib/vim-flavor.rb
Instance Attribute Summary collapse
-
#base_version ⇒ Object
readonly
Returns the value of attribute base_version.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #compatible?(other_version_or_s) ⇒ Boolean
- #find_the_best_version(versions) ⇒ Object
-
#initialize(s) ⇒ VersionConstraint
constructor
A new instance of VersionConstraint.
- #parse(s) ⇒ Object
- #to_s ⇒ Object
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_version ⇒ Object (readonly)
Returns the value of attribute base_version.
20 21 22 |
# File 'lib/vim-flavor.rb', line 20 def base_version @base_version end |
#operator ⇒ Object (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
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_s ⇒ Object
26 27 28 |
# File 'lib/vim-flavor.rb', line 26 def to_s() "#{@operator} #{@base_version}" end |