Class: MrBump::Version
- Inherits:
-
Object
- Object
- MrBump::Version
- Includes:
- Comparable
- Defined in:
- lib/mr_bump/version.rb
Overview
This class enables comparison and bumping of sementic versions and conversion to and from strings
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.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #bump_major ⇒ Object
- #bump_major! ⇒ Object
- #bump_minor ⇒ Object
- #bump_minor! ⇒ Object
- #bump_patch ⇒ Object
- #bump_patch! ⇒ Object
-
#initialize(version_str) ⇒ Version
constructor
A new instance of Version.
- #to_s ⇒ Object
Constructor Details
#initialize(version_str) ⇒ Version
Returns a new instance of Version.
11 12 13 14 15 16 17 |
# File 'lib/mr_bump/version.rb', line 11 def initialize(version_str) regex = Regexp.new('^([0-9]+)(\.([0-9]+)(\.([0-9]*))?)?') numbers = version_str.match(regex).captures @major = numbers[0].to_i @minor = numbers.size > 2 ? numbers[2].to_i : 0 @patch = numbers.size > 4 ? numbers[4].to_i : 0 end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
10 11 12 |
# File 'lib/mr_bump/version.rb', line 10 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
10 11 12 |
# File 'lib/mr_bump/version.rb', line 10 def minor @minor end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
10 11 12 |
# File 'lib/mr_bump/version.rb', line 10 def patch @patch end |
Instance Method Details
#<=>(other) ⇒ Object
19 20 21 22 23 |
# File 'lib/mr_bump/version.rb', line 19 def <=>(other) major_com = major <=> other.major minor_com = major_com.zero? ? minor <=> other.minor : major_com minor_com.zero? ? patch <=> other.patch : minor_com end |
#bump_major ⇒ Object
29 30 31 |
# File 'lib/mr_bump/version.rb', line 29 def bump_major dup.bump_major! end |
#bump_major! ⇒ Object
33 34 35 36 37 38 |
# File 'lib/mr_bump/version.rb', line 33 def bump_major! @major += 1 @minor = 0 @patch = 0 self end |
#bump_minor ⇒ Object
40 41 42 |
# File 'lib/mr_bump/version.rb', line 40 def bump_minor dup.bump_minor! end |
#bump_minor! ⇒ Object
44 45 46 47 48 |
# File 'lib/mr_bump/version.rb', line 44 def bump_minor! @minor += 1 @patch = 0 self end |
#bump_patch ⇒ Object
50 51 52 |
# File 'lib/mr_bump/version.rb', line 50 def bump_patch dup.bump_patch! end |
#bump_patch! ⇒ Object
54 55 56 57 |
# File 'lib/mr_bump/version.rb', line 54 def bump_patch! @patch += 1 self end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/mr_bump/version.rb', line 25 def to_s "#{major}.#{minor}.#{patch}" end |