Sha256: 1b757c94f0279e6743fa1b8989f3a9c2abd8285e6d8b7c499a545dd15ac73b9e

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

# The gem namespace.
module Versionaire
  module_function

  # Conversion function (strict) for casting an object into a version.
  # :reek:TooManyStatements
  def Version object
    converter = Converter.new object

    case object
      when String then converter.from_string
      when Array then converter.from_array
      when Hash then converter.from_hash
      when Version then object
      else converter.from_object
    end
  end

  # Aids with converting objects into valid versions.
  class Converter
    def initialize object
      @object = object
    end

    def from_string
      body = %(Use: "<major>.<minor>.<maintenance>" or "v<major>.<minor>.<maintenance>".)
      fail Errors::Conversion, error_message(object, body) unless Version.regex.match? object

      Version[string_to_arguments]
    end

    def from_array
      body = "Use: [], [<major>], [<major>, <minor>], or [<major>, <minor>, <maintenance>]."
      fail Errors::Conversion, error_message(object, body) unless (0..3).cover? object.size

      Version[array_to_arguments]
    end

    def from_hash
      body = "Use: {major: <major>}, " \
             "{major: <major>, minor: <minor>}, or " \
             "{major: <major>, minor: <minor>, maintenance: <maintenance>}."
      fail Errors::Conversion, error_message(object, body) unless required_keys?

      Version[object]
    end

    def from_object
      fail Errors::Conversion, error_message(object, "Use: String, Array, Hash, or Version.")
    end

    private

    attr_reader :object

    def string_to_arguments
      Version.arguments(*object.tr("v", "").split(".").map(&:to_i))
    end

    def array_to_arguments
      Version.arguments(*object.dup.fill(0, object.size..2))
    end

    def required_keys?
      object.keys.all? { |key| VERSION_ATTRIBUTES.include? key }
    end

    def error_message object, body
      "Invalid version conversion: #{object}. #{body}"
    end
  end

  private_constant :Converter
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
versionaire-7.3.4 lib/versionaire/conversion.rb
versionaire-7.3.3 lib/versionaire/conversion.rb