lib/versionaire/version.rb in versionaire-8.6.0 vs lib/versionaire/version.rb in versionaire-8.7.0

- old
+ new

@@ -1,14 +1,18 @@ # frozen_string_literal: true +require "refinements/structs" + module Versionaire DELIMITER = "." # An immutable, semantic version value object. Version = Struct.new :major, :minor, :patch, keyword_init: true do include Comparable + using Refinements::Structs + def self.regex / \A( # Start of string and OR. \d* # Major only. | # OR pipe. @@ -28,16 +32,20 @@ super validate freeze end + def []= key, value + super(key, value).tap { validate } + end + def + other - self.class.then { |klass| klass.new(**klass.arguments(*reduce(other, :+))) } + revalue(other.to_h) { |previous, current| previous + current } end def - other - self.class.then { |klass| klass.new(**klass.arguments(*reduce(other, :-))) } + revalue(other.to_h) { |previous, current| previous - current } end def == other hash == other.hash end @@ -46,27 +54,28 @@ def <=> other to_s <=> other.to_s end + def down key, value = 1 + revalue(key => value) { |previous, current| previous - current } + end + + def up key, value = 1 + revalue(key => value) { |previous, current| previous + current } + end + def to_s to_a.join DELIMITER end alias_method :to_str, :to_s + alias_method :values, :to_a - def to_a - [major, minor, patch] - end - private def validate fail Errors::InvalidNumber if to_a.any? { |number| !number.is_a? Integer } fail Errors::NegativeNumber if to_a.any?(&:negative?) - end - - def reduce other, action - to_a.zip(other.to_a).map { |pair| pair.reduce action } end end end