-
# frozen_string_literal: true
-
-
1
require "refinements/array"
-
-
1
module Versionaire
-
# An immutable, semantic version value object.
-
1
Version = Data.define :major, :minor, :patch do
-
1
include Comparable
-
-
1
using Refinements::Array
-
-
1
def initialize major: 0, minor: 0, patch: 0
-
191
super
-
191
validate
-
end
-
-
1
def +(other) = add other
-
-
1
def -(other) = substract other
-
-
1
def ==(other) = hash == other.hash
-
-
1
alias_method :eql?, :==
-
-
1
def <=>(other) = to_s <=> other.to_s
-
-
1
def down(key, value = 1) = substract({key => value})
-
-
1
def up(key, value = 1) = add({key => value})
-
-
1
def bump key
-
4
when: 1
case key
-
1
when: 1
when :major then bump_major
-
1
when: 1
when :minor then bump_minor
-
1
else: 1
when :patch then bump_patch
-
1
else fail Error, %(Invalid key: #{key.inspect}. Use: #{members.to_sentence "or"}.)
-
end
-
end
-
-
1
def inspect = to_s.inspect
-
-
1
def kind
-
4
then: 1
else: 3
if major? then :major
-
3
then: 1
else: 2
elsif minor? then :minor
-
2
then: 1
else: 1
elsif patch? then :patch
-
1
else :nascent
-
end
-
end
-
-
1
def major? = major.positive? && minor.zero? && patch.zero?
-
-
1
def minor?
-
8
nonmajor = major.zero?
-
8
nonpatch = patch.zero?
-
-
8
!(nonmajor && minor.zero? && nonpatch) &&
-
6
(nonmajor || major.positive?) && minor.positive? && nonpatch
-
end
-
-
1
def patch? = patch.positive?
-
-
1
def to_proc = method(:public_send).to_proc
-
-
1
def to_s = to_a.join DELIMITER
-
-
1
alias_method :to_str, :to_s
-
-
1
alias_method :to_a, :deconstruct
-
-
1
private
-
-
1
def validate
-
191
else: 190
then: 1
fail Error, "Major, minor, and patch must be a number." unless to_a.all? Integer
-
190
then: 3
else: 187
fail Error, "Major, minor, and patch must be a positive number." if to_a.any?(&:negative?)
-
end
-
-
1
def add other
-
5
attributes = other.to_h
-
12
attributes.each { |key, value| attributes[key] = public_send(key) + value }
-
5
with(**attributes)
-
end
-
-
1
def substract other
-
7
attributes = other.to_h
-
18
attributes.each { |key, value| attributes[key] = public_send(key) - value }
-
7
with(**attributes)
-
end
-
-
1
def bump_major = with major: major + 1, minor: 0, patch: 0
-
-
1
def bump_minor = with major:, minor: minor + 1, patch: 0
-
-
1
def bump_patch = with major:, minor:, patch: patch + 1
-
end
-
end