# 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. module Conversions require_relative "comparable_version" # The `ComparableVersion()` conversion method is defined as a module_function so that it # may also be called directly without needing to include the Conversions module # if so desired. # # @example # Conversions.ComparableVersion(1.2).to_s # => "1.2" module_function # Strict conversion method for creating a `ComparableVersion` object out of anything # that sensibly is a ComparableVersion. # # @param [Object] value the object to be converted # # @example # ComparableVersion(1) # => # # ComparableVersion(1.2) # => # # ComparableVersion("1.2.3") # => # # ComparableVersion(["1", "2", "3", "4"]) # => # 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 end