Class: Semantic::Version
- Inherits:
-
Object
- Object
- Semantic::Version
- Includes:
- Comparable
- 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
- #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.
- #satisfied_by?(versions) ⇒ Boolean
- #satisfies?(other_version) ⇒ Boolean
- #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.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/semantic/version.rb', line 12 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.
10 11 12 |
# File 'lib/semantic/version.rb', line 10 def build @build end |
#major ⇒ Object
Returns the value of attribute major.
9 10 11 |
# File 'lib/semantic/version.rb', line 9 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
9 10 11 |
# File 'lib/semantic/version.rb', line 9 def minor @minor end |
#patch ⇒ Object
Returns the value of attribute patch.
9 10 11 |
# File 'lib/semantic/version.rb', line 9 def patch @patch end |
#pre ⇒ Object
Returns the value of attribute pre.
9 10 11 |
# File 'lib/semantic/version.rb', line 9 def pre @pre end |
Instance Method Details
#<=>(other_version) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/semantic/version.rb', line 83 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 |
#compare_pre(prea, preb) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/semantic/version.rb', line 35 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
79 80 81 |
# File 'lib/semantic/version.rb', line 79 def eql? other_version self.hash == other_version.hash end |
#hash ⇒ Object
75 76 77 |
# File 'lib/semantic/version.rb', line 75 def hash to_a.hash end |
#identifiers(pre) ⇒ Object
29 30 31 32 33 |
# File 'lib/semantic/version.rb', line 29 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
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/semantic/version.rb', line 123 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 |
#satisfied_by?(versions) ⇒ Boolean
114 115 116 117 |
# File 'lib/semantic/version.rb', line 114 def satisfied_by? versions raise ArgumentError.new("Versions #{versions} should be an array of versions") unless versions.is_a? Array versions.all? { |version| satisfies?(version) } end |
#satisfies?(other_version) ⇒ Boolean
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/semantic/version.rb', line 94 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) elsif comparator == '~>' pessimistic_match? other_version_string else tilde_matches? other_version_string end end end |
#to_a ⇒ Object Also known as: to_array
55 56 57 |
# File 'lib/semantic/version.rb', line 55 def to_a [@major, @minor, @patch, @pre, @build] end |
#to_h ⇒ Object Also known as: to_hash
66 67 68 69 |
# File 'lib/semantic/version.rb', line 66 def to_h keys = [:major, :minor, :patch, :pre, :build] Hash[keys.zip(self.to_a)] end |
#to_s ⇒ Object Also known as: to_string
59 60 61 62 63 64 |
# File 'lib/semantic/version.rb', line 59 def to_s str = [@major, @minor, @patch].join '.' str << '-' << @pre unless @pre.nil? str << '+' << @build unless @build.nil? str end |