Module: Conversions

Defined in:
lib/version_compare/conversions.rb

Overview

Conversions is meant to be a common module used to define standard conversion methods. Anytime one of the standard conversion methods are needed, the Conversions module can be included and then used freely.

Class Method Summary collapse

Class Method Details

.ComparableVersion(value) ⇒ Object

Strict conversion method for creating a ‘ComparableVersion` object out of anything that sensibly is a ComparableVersion.

Examples:

ComparableVersion(1) # => #<ComparableVersion:0x007fd8144ea658 @major=1, @minor=nil, @tiny=nil, @patch=nil>
ComparableVersion(1.2) # => #<ComparableVersion:0x007fd8144ea658 @major=1, @minor=2, @tiny=nil, @patch=nil>
ComparableVersion("1.2.3") # => #<ComparableVersion:0x007fd8144ea658 @major=1, @minor=2, @tiny=3, @patch=nil>
ComparableVersion(["1", "2", "3", "4"]) # => #<ComparableVersion:0x007fd8144f98b0 @major=1, @minor=2, @tiny=3, @patch=4>

Parameters:

  • value (Object)

    the object to be converted



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/version_compare/conversions.rb', line 25

def ComparableVersion(value)
  case value
  when String,
       Integer,
       Float,
       -> val { val.respond_to?(:to_ary) }
    ComparableVersion.new(value)
  when -> val { val.respond_to?(:to_comparable_version) }
    value.to_comparable_version
  else
    raise TypeError, "Cannot convert #{value.inspect} to ComparableVersion"
  end
end