Class: Version
- Inherits:
-
Array
- Object
- Array
- Version
- Includes:
- Comparable
- Defined in:
- lib/caruby/helpers/version.rb
Overview
A Version is an Array of version major and minor components that is comparable to another version identifier based on a precedence relationship.
Instance Attribute Summary collapse
-
#predecessor ⇒ Object
readonly
Returns the value of attribute predecessor.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Returns the comparison of this version identifier to the other version identifier as follows: * if this version can be compared to other via the predecessor graph, then return that comparison result * otherwise, return a component-wise comparison.
-
#initialize(*params) ⇒ Version
constructor
Creates a new Version from the given version components and optional predecessor.
Constructor Details
#initialize(*params) ⇒ Version
Creates a new Version from the given version components and optional predecessor.
13 14 15 16 |
# File 'lib/caruby/helpers/version.rb', line 13 def initialize(*params) @predecessor = params.pop if self.class === params.last super(params) end |
Instance Attribute Details
#predecessor ⇒ Object (readonly)
Returns the value of attribute predecessor.
6 7 8 |
# File 'lib/caruby/helpers/version.rb', line 6 def predecessor @predecessor end |
Instance Method Details
#<=>(other) ⇒ Object
Returns the comparison of this version identifier to the other version identifier as follows:
-
if this version can be compared to other via the predecessor graph, then return that comparison result
-
otherwise, return a component-wise comparison
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/caruby/helpers/version.rb', line 27 def <=>(other) return 0 if equal?(other) raise ArgumentError.new("Comparand is not a #{self.class}: #{other}") unless self.class === other return -1 if other.predecessor == self return 1 unless predecessor.nil? or predecessor < other each_with_index do |component, index| return 1 unless index < other.length other_component = other[index] if String === other_component then component = component.to_s elsif String === component other_component = other_component.to_s end cmp = (component <=> other_component) return cmp unless cmp.zero? end length < other.length ? -1 : 0 end |