Class: VersionCompare::ComparableVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/version_compare/comparable_version.rb

Overview

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

Constant Summary collapse

DEFAULT_SEPARATOR =
".".freeze
NAMES =
%i[major minor tiny patch].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, separator: DEFAULT_SEPARATOR) ⇒ ComparableVersion

Returns a new instance of ComparableVersion.



15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



13
14
15
# File 'lib/version_compare/comparable_version.rb', line 13

def separator
  @separator
end

#value#to_comparable_version, ...

the Version object

Returns:



6
7
8
# File 'lib/version_compare/comparable_version.rb', line 6

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

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.



62
63
64
65
66
67
68
69
# 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

#inspectObject



28
29
30
# File 'lib/version_compare/comparable_version.rb', line 28

def inspect
  "<#{identification}>"
end

#to_aArray Also known as: to_ary

Returns an Array representation of the version’s parts.

Returns:

  • (Array)

    an Array representation of the version’s parts



46
47
48
# File 'lib/version_compare/comparable_version.rb', line 46

def to_a
  NAMES.map { |name| public_send(name) }.compact
end

#to_comparable_versionVersionCompare::ComparableVersion

Implicit conversion method.

Returns:



35
36
37
# File 'lib/version_compare/comparable_version.rb', line 35

def to_comparable_version
  self
end

#to_sString Also known as: to_str

Returns a String representation of the version.

Returns:

  • (String)

    a String representation of the version



40
41
42
# File 'lib/version_compare/comparable_version.rb', line 40

def to_s
  NAMES.map { |name| public_send(name) }.compact.join(separator)
end