lib/versionaire/function.rb in versionaire-10.6.0 vs lib/versionaire/function.rb in versionaire-11.0.0
- old
+ new
@@ -6,11 +6,10 @@
# The gem namespace.
module Versionaire
module_function
# Conversion function (strict) for casting an object into a version.
- # :reek:TooManyStatements
def Version object
Converter.new(object).then do |converter|
case object
when String then converter.from_string
when Array then converter.from_array
@@ -24,26 +23,27 @@
# Aids with converting objects into valid versions.
class Converter
using Refinements::Arrays
using Refinements::Structs
- def initialize object
+ def initialize object, model: Version
@object = object
+ @model = model
end
def from_string
body = "Use: <major>.<minor>.<patch>, <major>.<minor>, <major>, or empty string."
- fail Error, error_message(object, body) unless Version.pattern.match? object
+ fail Error, error_message(object, body) unless PATTERN.match? object
string_to_version
end
def from_array
body = "Use: [<major>, <minor>, <patch>], [<major>, <minor>], [<major>], or []."
fail Error, error_message(object, body) unless (0..3).cover? object.size
- Version.with_positions(*object.pad(0, max: 3))
+ model.with_positions(*object.pad(0, max: 3))
end
def from_hash
body = "Use: {major: <major>, minor: <minor>, patch: <patch>}, " \
"{major: <major>, minor: <minor>}, {major: <major>}, or {}."
@@ -56,19 +56,19 @@
fail Error, error_message(object, "Use: String, Array, Hash, or Version.")
end
private
- attr_reader :object, :filler
+ attr_reader :object, :model
def string_to_version
- object.split(Version.delimiter)
+ object.split(DELIMITER)
.map(&:to_i)
.then { |numbers| numbers.pad 0, max: 3 }
- .then { |arguments| Version.with_positions(*arguments) }
+ .then { |arguments| model.with_positions(*arguments) }
end
- def required_keys? = object.keys.all? { |key| Version.members.include? key }
+ 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