Class: Gversion
Constant Summary collapse
- PATTERN =
/^(?<major>0|[0-9][0-9]*)(\.(?<minor>0|[0-9][0-9]*))?(\.(?<revision>0|[0-9][0-9]*))?$/
Instance Attribute Summary collapse
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
Class Method Summary collapse
- .[](*array) ⇒ Object
- .greater_or_equal_than_version?(version, from_version) ⇒ Boolean
- .in_version_range?(version, from_version, to_version) ⇒ Boolean
- .lesser_than_version?(version, from_version) ⇒ Boolean
- .parse(string) ⇒ Object
- .parse!(string, _options = {}) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(options) ⇒ Gversion
constructor
A new instance of Gversion.
- #to_f ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ Gversion
Returns a new instance of Gversion.
8 9 10 11 12 13 |
# File 'lib/gversion.rb', line 8 def initialize() [:major, :minor, :revision].each do |field| validate_field(field, [field]) instance_variable_set("@#{field}", [field] || 0) end end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
6 7 8 |
# File 'lib/gversion.rb', line 6 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
6 7 8 |
# File 'lib/gversion.rb', line 6 def minor @minor end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
6 7 8 |
# File 'lib/gversion.rb', line 6 def revision @revision end |
Class Method Details
.[](*array) ⇒ Object
22 23 24 25 26 |
# File 'lib/gversion.rb', line 22 def self.[](*array) new(major: array[0].to_i, minor: array[1].to_i, revision: array[2].to_i) end |
.greater_or_equal_than_version?(version, from_version) ⇒ Boolean
51 52 53 |
# File 'lib/gversion.rb', line 51 def self.greater_or_equal_than_version?(version, from_version) self.parse!(version) >= self.parse(from_version) end |
.in_version_range?(version, from_version, to_version) ⇒ Boolean
59 60 61 62 63 64 65 |
# File 'lib/gversion.rb', line 59 def self.in_version_range?(version, from_version, to_version) v = self.parse!(version) min = self.parse!(from_version) max = self.parse!(to_version) v.between?(min, max) end |
.lesser_than_version?(version, from_version) ⇒ Boolean
55 56 57 |
# File 'lib/gversion.rb', line 55 def self.lesser_than_version?(version, from_version) !self.greater_or_equal_than_version?(version, from_version) end |
.parse(string) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/gversion.rb', line 15 def self.parse(string) match = PATTERN.match(string) return nil unless match self[match[:major], match[:minor], match[:revision]] end |
.parse!(string, _options = {}) ⇒ Object
28 29 30 |
# File 'lib/gversion.rb', line 28 def self.parse!(string, = {}) parse(string) || (fail ArgumentError, 'invalid version format') end |
Instance Method Details
#<=>(other) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/gversion.rb', line 32 def <=>(other) comparison = 0 [:major, :minor, :revision].each do |field| comparison = send(field) <=> other.send(field) break if comparison != 0 end comparison end |
#to_f ⇒ Object
46 47 48 49 |
# File 'lib/gversion.rb', line 46 def to_f # Will be wrong float number if minor > 9 @as_float ||= [major, minor].join('.').to_f end |
#to_s ⇒ Object
42 43 44 |
# File 'lib/gversion.rb', line 42 def to_s @as_string ||= [major, minor, revision].join('.') end |