Class: Semantic::Version
- Inherits:
-
Object
- Object
- Semantic::Version
- Defined in:
- lib/semantic/version.rb
Overview
See: semver.org
Constant Summary collapse
- SemVerRegexp =
/^(\d+\.\d+\.\d+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-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
- #<=(other_version) ⇒ Object
- #<=>(other_version) ⇒ Object
- #==(other_version) ⇒ Object
- #>(other_version) ⇒ Object
- #>=(other_version) ⇒ Object
-
#initialize(version_str) ⇒ Version
constructor
A new instance of Version.
- #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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/semantic/version.rb', line 6 def initialize version_str raise ArgumentError.new("Not a valid SemVer Version (http://semver.org)") unless version_str =~ SemVerRegexp version, parts = version_str.split '-' if not parts.nil? and parts.include? '+' @pre, @build = parts.split '+' elsif version.include? '+' version, @build = version.split '+' else @pre = parts end @major, @minor, @patch = version.split('.').map &:to_i end |
Instance Attribute Details
#build ⇒ Object
Returns the value of attribute build.
4 5 6 |
# File 'lib/semantic/version.rb', line 4 def build @build end |
#major ⇒ Object
Returns the value of attribute major.
4 5 6 |
# File 'lib/semantic/version.rb', line 4 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
4 5 6 |
# File 'lib/semantic/version.rb', line 4 def minor @minor end |
#patch ⇒ Object
Returns the value of attribute patch.
4 5 6 |
# File 'lib/semantic/version.rb', line 4 def patch @patch end |
#pre ⇒ Object
Returns the value of attribute pre.
4 5 6 |
# File 'lib/semantic/version.rb', line 4 def pre @pre end |
Instance Method Details
#<(other_version) ⇒ Object
52 53 54 |
# File 'lib/semantic/version.rb', line 52 def < other_version if (self <=> other_version) == -1 then true else false end end |
#<=(other_version) ⇒ Object
60 61 62 |
# File 'lib/semantic/version.rb', line 60 def <= other_version if (self <=> other_version) <= 0 then true else false end end |
#<=>(other_version) ⇒ Object
43 44 45 46 |
# File 'lib/semantic/version.rb', line 43 def <=> other_version other_version = Version.new(other_version) if other_version.is_a? String compare_recursively self.to_a.dup, other_version.to_a.dup end |
#==(other_version) ⇒ Object
64 65 66 |
# File 'lib/semantic/version.rb', line 64 def == other_version if (self <=> other_version) == 0 then true else false end end |
#>(other_version) ⇒ Object
48 49 50 |
# File 'lib/semantic/version.rb', line 48 def > other_version if (self <=> other_version) == 1 then true else false end end |
#>=(other_version) ⇒ Object
56 57 58 |
# File 'lib/semantic/version.rb', line 56 def >= other_version if (self <=> other_version) >= 0 then true else false end end |
#to_a ⇒ Object Also known as: to_array
22 23 24 |
# File 'lib/semantic/version.rb', line 22 def to_a [@major, @minor, @patch, @pre, @build] end |
#to_h ⇒ Object Also known as: to_hash
34 35 36 37 |
# File 'lib/semantic/version.rb', line 34 def to_h keys = [:major, :minor, :patch, :pre, :build] Hash[keys.zip(self.to_a)] end |
#to_s ⇒ Object Also known as: to_string
26 27 28 29 30 31 32 |
# File 'lib/semantic/version.rb', line 26 def to_s str = [@major, @minor, @patch].join '.' str << '-' << @pre unless @pre.nil? str << '+' << @build unless @build.nil? str end |