Class: TRuby::SemanticVersion
- Inherits:
-
Object
- Object
- TRuby::SemanticVersion
- Includes:
- Comparable
- Defined in:
- lib/t_ruby/package_manager.rb
Overview
Semantic version parsing and comparison
Constant Summary collapse
- VERSION_REGEX =
/^(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/.freeze
Instance Attribute Summary collapse
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
-
#prerelease ⇒ Object
readonly
Returns the value of attribute prerelease.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(version_string) ⇒ SemanticVersion
constructor
A new instance of SemanticVersion.
- #satisfies?(constraint) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(version_string) ⇒ SemanticVersion
Returns a new instance of SemanticVersion.
17 18 19 20 21 22 23 24 25 |
# File 'lib/t_ruby/package_manager.rb', line 17 def initialize(version_string) match = VERSION_REGEX.match(version_string.to_s) raise ArgumentError, "Invalid version: #{version_string}" unless match @major = match[1].to_i @minor = match[2].to_i @patch = match[3].to_i @prerelease = match[4] end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
13 14 15 |
# File 'lib/t_ruby/package_manager.rb', line 13 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
13 14 15 |
# File 'lib/t_ruby/package_manager.rb', line 13 def minor @minor end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
13 14 15 |
# File 'lib/t_ruby/package_manager.rb', line 13 def patch @patch end |
#prerelease ⇒ Object (readonly)
Returns the value of attribute prerelease.
13 14 15 |
# File 'lib/t_ruby/package_manager.rb', line 13 def prerelease @prerelease end |
Class Method Details
.parse(str) ⇒ Object
50 51 52 53 54 |
# File 'lib/t_ruby/package_manager.rb', line 50 def self.parse(str) new(str) rescue ArgumentError nil end |
Instance Method Details
#<=>(other) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/t_ruby/package_manager.rb', line 27 def <=>(other) return nil unless other.is_a?(SemanticVersion) result = [@major, @minor, @patch] <=> [other.major, other.minor, other.patch] return result unless result.zero? # Both have same version, compare prerelease return 0 if @prerelease.nil? && other.prerelease.nil? return 1 if @prerelease.nil? # Release > prerelease return -1 if other.prerelease.nil? @prerelease <=> other.prerelease end |
#satisfies?(constraint) ⇒ Boolean
41 42 43 |
# File 'lib/t_ruby/package_manager.rb', line 41 def satisfies?(constraint) VersionConstraint.new(constraint).satisfied_by?(self) end |
#to_s ⇒ Object
45 46 47 48 |
# File 'lib/t_ruby/package_manager.rb', line 45 def to_s base = "#{@major}.#{@minor}.#{@patch}" @prerelease ? "#{base}-#{@prerelease}" : base end |