Class: SL::SemVer
- Inherits:
-
Object
- Object
- SL::SemVer
- Defined in:
- lib/searchlink/semver.rb
Overview
Semantic versioning library
Instance Attribute Summary collapse
-
#maj ⇒ Object
Returns the value of attribute maj.
-
#min ⇒ Object
Returns the value of attribute min.
-
#patch ⇒ Object
Returns the value of attribute patch.
-
#pre ⇒ Object
Returns the value of attribute pre.
Instance Method Summary collapse
- #<(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
-
#equal?(other) ⇒ Boolean
Test if self is equal to other.
-
#initialize(version_string) ⇒ SemVer
constructor
Initialize a Semantic Version object.
- #inspect ⇒ Object
-
#newer_than(other) ⇒ Boolean
Test if self is newer than a semantic version number.
-
#older_than(other) ⇒ Boolean
Test if self is older than a semantic version number.
- #to_s ⇒ Object
Constructor Details
#initialize(version_string) ⇒ SemVer
Initialize a Semantic Version object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/searchlink/semver.rb', line 14 def initialize(version_string) raise VersionError.new("Invalid semantic version number: #{version_string}") unless version_string.valid_version? @maj, @min, @patch = version_string.split(/\./) @pre = nil if @patch =~ /(-?[^0-9]+\d*)$/ @pre = Regexp.last_match(1).sub(/^-/, '') @patch = @patch.sub(/(-?[^0-9]+\d*)$/, '') end @maj = @maj.to_i @min = @min.to_i @patch = @patch.to_i end |
Instance Attribute Details
#maj ⇒ Object
Returns the value of attribute maj.
6 7 8 |
# File 'lib/searchlink/semver.rb', line 6 def maj @maj end |
#min ⇒ Object
Returns the value of attribute min.
6 7 8 |
# File 'lib/searchlink/semver.rb', line 6 def min @min end |
#patch ⇒ Object
Returns the value of attribute patch.
6 7 8 |
# File 'lib/searchlink/semver.rb', line 6 def patch @patch end |
#pre ⇒ Object
Returns the value of attribute pre.
6 7 8 |
# File 'lib/searchlink/semver.rb', line 6 def pre @pre end |
Instance Method Details
#<(other) ⇒ Object
83 84 85 |
# File 'lib/searchlink/semver.rb', line 83 def <(other) older_than(other) end |
#==(other) ⇒ Object
123 124 125 |
# File 'lib/searchlink/semver.rb', line 123 def ==(other) equal?(other) end |
#>(other) ⇒ Object
103 104 105 |
# File 'lib/searchlink/semver.rb', line 103 def >(other) newer_than(other) end |
#equal?(other) ⇒ Boolean
Test if self is equal to other
114 115 116 117 118 |
# File 'lib/searchlink/semver.rb', line 114 def equal?(other) v = other.is_a?(SemVer) ? other : SemVer.new(other) v.maj == @maj && v.min == @min && v.patch == @patch && v.pre == @pre end |
#inspect ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/searchlink/semver.rb', line 127 def inspect { object_id: object_id, maj: @maj, min: @min, patch: @patch, pre: @pre } end |
#newer_than(other) ⇒ Boolean
Test if self is newer than a semantic version number
95 96 97 98 |
# File 'lib/searchlink/semver.rb', line 95 def newer_than(other) v = other.is_a?(SemVer) ? other : SemVer.new(other) v.older_than(self) && !v.equal?(self) end |
#older_than(other) ⇒ Boolean
Test if self is older than a semantic version number
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/searchlink/semver.rb', line 52 def older_than(other) latest = other.is_a?(SemVer) ? other : SemVer.new(other) return false if latest.equal?(self) if @maj > latest.maj false elsif @maj < latest.maj true elsif @min > latest.min false elsif @min < latest.min true elsif @patch > latest.patch false elsif @patch < latest.patch true else return false if @pre.nil? && latest.pre.nil? return true if @pre.nil? && !latest.pre.nil? return false if !@pre.nil? && latest.pre.nil? @pre < latest.pre end end |
#to_s ⇒ Object
137 138 139 140 |
# File 'lib/searchlink/semver.rb', line 137 def to_s ver = [@maj, @min, @patch].join('.') @pre.nil? ? ver : "#{ver}-#{@pre}" end |