class Versionaire::Converter

Aids with converting objects into valid versions.

Attributes

model[R]
object[R]

Public Class Methods

new(object, model: Version) click to toggle source
# File lib/versionaire/function.rb, line 28
def initialize object, model: Version
  @object = object
  @model = model
end

Public Instance Methods

from_array() click to toggle source
# File lib/versionaire/function.rb, line 40
def from_array
  body = "Use: [<major>, <minor>, <patch>], [<major>, <minor>], [<major>], or []."
  fail Error, error_message(object, body) unless (0..3).cover? object.size

  model.with_positions(*object.pad(0, 3))
end
from_hash() click to toggle source
# File lib/versionaire/function.rb, line 47
def from_hash
  body = "Use: {major: <major>, minor: <minor>, patch: <patch>}, " \
         "{major: <major>, minor: <minor>}, {major: <major>}, or {}."
  fail Error, error_message(object, body) unless required_keys?

  Version[**object]
end
from_object() click to toggle source
# File lib/versionaire/function.rb, line 55
def from_object
  fail Error, error_message(object, "Use: String, Array, Hash, or Version.")
end
from_string() click to toggle source
# File lib/versionaire/function.rb, line 33
def from_string
  body = "Use: <major>.<minor>.<patch>, <major>.<minor>, <major>, or empty string."
  fail Error, error_message(object, body) unless PATTERN.match? object

  string_to_version
end

Private Instance Methods

error_message(object, body) click to toggle source
# File lib/versionaire/function.rb, line 72
  def error_message(object, body) = "Invalid version conversion: #{object}. #{body}"
end
required_keys?(= object.keys.all? { |key| model.members.include? key }) click to toggle source
# File lib/versionaire/function.rb, line 70
    def required_keys? = object.keys.all? { |key| model.members.include? key }

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

  private_constant :Converter
end
string_to_version() click to toggle source
# File lib/versionaire/function.rb, line 63
def string_to_version
  object.split(DELIMITER)
        .map(&:to_i)
        .then { |numbers| numbers.pad 0, 3 }
        .then { |arguments| model.with_positions(*arguments) }
end