Class: MilkTea::PackageVersion
- Inherits:
-
Object
- Object
- MilkTea::PackageVersion
- Includes:
- Comparable
- Defined in:
- lib/milk_tea/packages/version.rb
Constant Summary collapse
- VERSION_PATTERN =
/\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\z/
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.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(major, minor, patch) ⇒ PackageVersion
constructor
A new instance of PackageVersion.
- #to_s ⇒ Object
Constructor Details
#initialize(major, minor, patch) ⇒ PackageVersion
Returns a new instance of PackageVersion.
28 29 30 31 32 |
# File 'lib/milk_tea/packages/version.rb', line 28 def initialize(major, minor, patch) @major = Integer(major) @minor = Integer(minor) @patch = Integer(patch) end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
11 12 13 |
# File 'lib/milk_tea/packages/version.rb', line 11 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
11 12 13 |
# File 'lib/milk_tea/packages/version.rb', line 11 def minor @minor end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
11 12 13 |
# File 'lib/milk_tea/packages/version.rb', line 11 def patch @patch end |
Class Method Details
.parse(value, label: "package version") ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/milk_tea/packages/version.rb', line 13 def self.parse(value, label: "package version") return value if value.is_a?(self) text = value.to_s.strip raise PackageVersionError, "#{label} cannot be empty" if text.empty? match = VERSION_PATTERN.match(text) unless match raise PackageVersionError, "#{label} #{text.inspect} must use semantic version format MAJOR.MINOR.PATCH" end new(match[1].to_i, match[2].to_i, match[3].to_i) end |
Instance Method Details
#<=>(other) ⇒ Object
34 35 36 37 38 |
# File 'lib/milk_tea/packages/version.rb', line 34 def <=>(other) return nil unless other.is_a?(PackageVersion) [major, minor, patch] <=> [other.major, other.minor, other.patch] end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/milk_tea/packages/version.rb', line 40 def to_s "#{major}.#{minor}.#{patch}" end |