lib/versionaire/function.rb in versionaire-13.8.0 vs lib/versionaire/function.rb in versionaire-13.9.0
- old
+ new
@@ -1,9 +1,8 @@
# frozen_string_literal: true
require "refinements/array"
-require "refinements/struct"
# The gem namespace.
module Versionaire
module_function
@@ -21,11 +20,10 @@
end
# Aids with converting objects into valid versions.
class Converter
using Refinements::Array
- using Refinements::Struct
def initialize object, model: Version
@object = object
@model = model
end
@@ -39,11 +37,11 @@
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))
+ model.new(**attributes_for(object.pad(0, 3)))
end
def from_hash
body = "Use: {major: <major>, minor: <minor>, patch: <patch>}, " \
"{major: <major>, minor: <minor>}, {major: <major>}, or {}."
@@ -62,11 +60,13 @@
def string_to_version
object.split(DELIMITER)
.map(&:to_i)
.then { |numbers| numbers.pad 0, 3 }
- .then { |arguments| model.with_positions(*arguments) }
+ .then { |values| model.new(**attributes_for(values)) }
end
+
+ def attributes_for(values) = model.members.zip(values).to_h
def required_keys? = object.keys.all? { |key| model.members.include? key }
def error_message(object, body) = "Invalid version conversion: #{object}. #{body}"
end