Class: Semantic::Version
- Inherits:
-
Object
- Object
- Semantic::Version
- Defined in:
- lib/semantic/version.rb
Constant Summary collapse
- SemVerRegexp =
/\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?\Z/
Instance Attribute Summary collapse
-
#build ⇒ Object
Returns the value of attribute build.
-
#major ⇒ Object
Returns the value of attribute major.
-
#minor ⇒ Object
Returns the value of attribute minor.
-
#patch ⇒ Object
Returns the value of attribute patch.
-
#pre ⇒ Object
Returns the value of attribute pre.
Instance Method Summary collapse
- #<(other_version) ⇒ Object
- #<=(other_version) ⇒ Object
- #<=>(other_version) ⇒ Object
- #==(other_version) ⇒ Object
- #>(other_version) ⇒ Object
- #>=(other_version) ⇒ Object
- #compare_pre(prea, preb) ⇒ Object
- #eql?(other_version) ⇒ Boolean
- #hash ⇒ Object
- #identifiers(pre) ⇒ Object
- #increment!(term) ⇒ Object
-
#initialize(version_str) ⇒ Version
constructor
A new instance of Version.
- #satisfies(other_version) ⇒ Object
- #to_a ⇒ Object (also: #to_array)
- #to_h ⇒ Object (also: #to_hash)
- #to_s ⇒ Object (also: #to_string)
Constructor Details
#initialize(version_str) ⇒ Version
Returns a new instance of Version.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/semantic/version.rb', line 9 def initialize version_str v = version_str.match(SemVerRegexp) raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") if v.nil? @major = v[1].to_i @minor = v[2].to_i @patch = v[3].to_i @pre = v[4] @build = v[5] @version = version_str end |
Instance Attribute Details
#build ⇒ Object
Returns the value of attribute build.
7 8 9 |
# File 'lib/semantic/version.rb', line 7 def build @build end |
#major ⇒ Object
Returns the value of attribute major.
7 8 9 |
# File 'lib/semantic/version.rb', line 7 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
7 8 9 |
# File 'lib/semantic/version.rb', line 7 def minor @minor end |
#patch ⇒ Object
Returns the value of attribute patch.
7 8 9 |
# File 'lib/semantic/version.rb', line 7 def patch @patch end |
#pre ⇒ Object
Returns the value of attribute pre.
7 8 9 |
# File 'lib/semantic/version.rb', line 7 def pre @pre end |
Instance Method Details
#<(other_version) ⇒ Object
95 96 97 |
# File 'lib/semantic/version.rb', line 95 def < other_version (self <=> other_version) == -1 end |
#<=(other_version) ⇒ Object
103 104 105 |
# File 'lib/semantic/version.rb', line 103 def <= other_version (self <=> other_version) <= 0 end |
#<=>(other_version) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/semantic/version.rb', line 80 def <=> other_version other_version = Version.new(other_version) if other_version.is_a? String [:major, :minor, :patch].each do |part| c = (self.send(part) <=> other_version.send(part)) if c != 0 return c end end return compare_pre(self.pre, other_version.pre) end |
#==(other_version) ⇒ Object
107 108 109 |
# File 'lib/semantic/version.rb', line 107 def == other_version (self <=> other_version) == 0 end |
#>(other_version) ⇒ Object
91 92 93 |
# File 'lib/semantic/version.rb', line 91 def > other_version (self <=> other_version) == 1 end |
#>=(other_version) ⇒ Object
99 100 101 |
# File 'lib/semantic/version.rb', line 99 def >= other_version (self <=> other_version) >= 0 end |
#compare_pre(prea, preb) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/semantic/version.rb', line 32 def compare_pre(prea, preb) if prea.nil? || preb.nil? return 0 if prea.nil? && preb.nil? return 1 if prea.nil? return -1 if preb.nil? end a = identifiers(prea) b = identifiers(preb) smallest = a.size < b.size ? a : b smallest.each_with_index do |e, i| c = a[i] <=> b[i] if c.nil? return a[i].is_a?(Integer) ? -1 : 1 elsif c != 0 return c end end return a.size <=> b.size end |
#eql?(other_version) ⇒ Boolean
76 77 78 |
# File 'lib/semantic/version.rb', line 76 def eql? other_version self.hash == other_version.hash end |
#hash ⇒ Object
72 73 74 |
# File 'lib/semantic/version.rb', line 72 def hash to_a.hash end |
#identifiers(pre) ⇒ Object
26 27 28 29 30 |
# File 'lib/semantic/version.rb', line 26 def identifiers(pre) array = pre.split(".") array.each_with_index {|e,i| array[i] = Integer(e) if /\A\d+\z/.match(e)} return array end |
#increment!(term) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/semantic/version.rb', line 133 def increment!(term) term = term.to_sym new_version = clone new_value = send(term) + 1 new_version.send("#{term}=", new_value) new_version.minor = 0 if term == :major new_version.patch = 0 if term == :major || term == :minor new_version.build = new_version.pre = nil new_version end |
#satisfies(other_version) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/semantic/version.rb', line 111 def satisfies other_version return true if other_version.strip == '*' parts = other_version.split(/(\d(.+)?)/, 2) comparator, other_version_string = parts[0].strip, parts[1].strip begin Version.new other_version_string comparator.empty? && comparator = '==' satisfies_comparator? comparator, other_version_string rescue ArgumentError if ['<', '>', '<=', '>='].include?(comparator) satisfies_comparator? comparator, pad_version_string(other_version_string) else tilde_matches? other_version_string end end end |
#to_a ⇒ Object Also known as: to_array
52 53 54 |
# File 'lib/semantic/version.rb', line 52 def to_a [@major, @minor, @patch, @pre, @build] end |
#to_h ⇒ Object Also known as: to_hash
63 64 65 66 |
# File 'lib/semantic/version.rb', line 63 def to_h keys = [:major, :minor, :patch, :pre, :build] Hash[keys.zip(self.to_a)] end |
#to_s ⇒ Object Also known as: to_string
56 57 58 59 60 61 |
# File 'lib/semantic/version.rb', line 56 def to_s str = [@major, @minor, @patch].join '.' str << '-' << @pre unless @pre.nil? str << '+' << @build unless @build.nil? str end |