class VersionCompare::ComparableVersion

VersionCompare::ComparableVersion objects compare themselves with other VersionCompare::ComparableVersion objects.

@attr value [#to_comparable_version, to_ary, to_s] the Version object

Constants

DEFAULT_SEPARATOR
NAMES

Attributes

separator[R]

Public Class Methods

new(value, separator: DEFAULT_SEPARATOR) click to toggle source
# File lib/version_compare/comparable_version.rb, line 15
def initialize(value, separator: DEFAULT_SEPARATOR)
  @separator = separator

  @major, @minor, @tiny, @patch =
    if value.respond_to?(:to_comparable_version)
      value.to_comparable_version.to_a
    elsif value.respond_to?(:to_ary)
      value.to_ary.map(&:to_i)
    else
      value.to_s.split(separator).map(&:to_i)
    end
end

Public Instance Methods

<=>(other) click to toggle source

ComparableVersion components comparison method. Uses Comparable to assess whether this ComparableVersion's component value or the other ComparableVersion's component value is greater or lesser. The first value to be found as greater or lesser determines which ComparableVersion object is greater or lesser.

Missing ComparableVersion components are treated as 0 values, which effectively gives them no weight in the comparison.

@params [ComparableVersion] other the other ComparableVersion object we

are comparing with
# File lib/version_compare/comparable_version.rb, line 62
def <=>(other)
  NAMES.each do |name|
    result = send(name).to_i <=> other.send(name).to_i
    return result unless result.zero?
  end

  0
end
inspect() click to toggle source
# File lib/version_compare/comparable_version.rb, line 28
def inspect
  "<#{identification}>"
end
to_a() click to toggle source

@return [Array] an Array representation of the version's parts

# File lib/version_compare/comparable_version.rb, line 46
def to_a
  NAMES.map { |name| public_send(name) }.compact
end
Also aliased as: to_ary
to_ary()
Alias for: to_a
to_comparable_version() click to toggle source

Implicit conversion method.

@return [VersionCompare::ComparableVersion] self

# File lib/version_compare/comparable_version.rb, line 35
def to_comparable_version
  self
end
to_s() click to toggle source

@return [String] a String representation of the version

# File lib/version_compare/comparable_version.rb, line 40
def to_s
  NAMES.map { |name| public_send(name) }.compact.join(separator)
end
Also aliased as: to_str
to_str()
Alias for: to_s

Private Instance Methods

identification() click to toggle source
# File lib/version_compare/comparable_version.rb, line 73
def identification
  version_identifiers =
    NAMES.map { |name|
      "#{name}:#{send(name)}" if send(name)
    }.compact.join(", ")

  "#{self.class}[#{version_identifiers}]"
end