Class: Versionaire::Version
- Inherits:
-
Object
- Object
- Versionaire::Version
- Includes:
- Comparable
- Defined in:
- lib/versionaire/version.rb
Overview
An immutable, semantic version value object.
Instance Attribute Summary collapse
-
#maintenance ⇒ Object
readonly
Returns the value of attribute maintenance.
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
Class Method Summary collapse
- .arguments(major, minor, maintenance) ⇒ Object
- .delimiter ⇒ Object
- .keys ⇒ Object
- .string_format ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(major: 0, minor: 0, maintenance: 0) ⇒ Version
constructor
A new instance of Version.
- #label ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(major: 0, minor: 0, maintenance: 0) ⇒ Version
Returns a new instance of Version.
35 36 37 38 39 40 |
# File 'lib/versionaire/version.rb', line 35 def initialize major: 0, minor: 0, maintenance: 0 @major = major @minor = minor @maintenance = maintenance validate end |
Instance Attribute Details
#maintenance ⇒ Object (readonly)
Returns the value of attribute maintenance.
8 9 10 |
# File 'lib/versionaire/version.rb', line 8 def maintenance @maintenance end |
#major ⇒ Object (readonly)
Returns the value of attribute major.
8 9 10 |
# File 'lib/versionaire/version.rb', line 8 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
8 9 10 |
# File 'lib/versionaire/version.rb', line 8 def minor @minor end |
Class Method Details
.arguments(major, minor, maintenance) ⇒ Object
31 32 33 |
# File 'lib/versionaire/version.rb', line 31 def self.arguments major, minor, maintenance Hash[keys.zip [major, minor, maintenance]] end |
.delimiter ⇒ Object
14 15 16 |
# File 'lib/versionaire/version.rb', line 14 def self.delimiter "." end |
.keys ⇒ Object
10 11 12 |
# File 'lib/versionaire/version.rb', line 10 def self.keys %i[major minor maintenance] end |
.string_format ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/versionaire/version.rb', line 18 def self.string_format / \A # Start of string. v? # Optional prefix. \d{1,} # Major version. #{delimiter} # Delimiter. \d{1,} # Minor version. #{delimiter} # Delimiter. \d{1,} # Maintenance version. \z # End of string. /x end |
Instance Method Details
#+(other) ⇒ Object
42 43 44 |
# File 'lib/versionaire/version.rb', line 42 def + other self.class.new self.class.arguments(*reduce(other, :+)) end |
#-(other) ⇒ Object
46 47 48 |
# File 'lib/versionaire/version.rb', line 46 def - other self.class.new self.class.arguments(*reduce(other, :-)) end |
#<=>(other) ⇒ Object
55 56 57 |
# File 'lib/versionaire/version.rb', line 55 def <=> other to_s <=> other.to_s end |
#==(other) ⇒ Object Also known as: eql?
50 51 52 |
# File 'lib/versionaire/version.rb', line 50 def == other other.is_a?(Version) && to_s == other.to_s end |
#hash ⇒ Object
59 60 61 |
# File 'lib/versionaire/version.rb', line 59 def hash [major, minor, maintenance, self.class].hash end |
#label ⇒ Object
63 64 65 |
# File 'lib/versionaire/version.rb', line 63 def label "v#{self}" end |
#to_a ⇒ Object
72 73 74 |
# File 'lib/versionaire/version.rb', line 72 def to_a [major, minor, maintenance] end |
#to_h ⇒ Object
76 77 78 |
# File 'lib/versionaire/version.rb', line 76 def to_h {major: major, minor: minor, maintenance: maintenance} end |
#to_s ⇒ Object Also known as: to_str
67 68 69 |
# File 'lib/versionaire/version.rb', line 67 def to_s "#{major}#{self.class.delimiter}#{minor}#{self.class.delimiter}#{maintenance}" end |