Sha256: 5ea898e659cec117837dab454bae4e34700fc86a2417ce63912c468ef05fabf3

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "refinements/structs"

module Versionaire
  # An immutable, semantic version value object.
  Version = Struct.new :major, :minor, :patch, keyword_init: true do
    include Comparable

    using Refinements::Structs

    def initialize major: 0, minor: 0, patch: 0
      super
      validate
      freeze
    end

    def []= key, value
      super(key, value).tap { validate }
    end

    def +(other) = revalue(other.to_h) { |previous, current| previous + current }

    def -(other) = revalue(other.to_h) { |previous, current| previous - current }

    def ==(other) = hash == other.hash

    alias_method :eql?, :==

    def <=>(other) = to_s <=> other.to_s

    def down(key, value = 1) = revalue(key => value) { |previous, current| previous - current }

    def up(key, value = 1) = revalue(key => value) { |previous, current| previous + current }

    def inspect = to_s.inspect

    def to_proc = method(:[]).to_proc

    def to_s = to_a.join DELIMITER

    alias_method :to_str, :to_s

    private

    def validate
      fail Error, "Major, minor, and patch must be a number." if to_a.any? do |number|
                                                                   !number.is_a? Integer
                                                                 end

      fail Error, "Major, minor, and patch must be a positive number." if to_a.any?(&:negative?)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
versionaire-12.0.1 lib/versionaire/version.rb
versionaire-12.0.0 lib/versionaire/version.rb
versionaire-11.0.3 lib/versionaire/version.rb
versionaire-11.0.2 lib/versionaire/version.rb
versionaire-11.0.1 lib/versionaire/version.rb
versionaire-11.0.0 lib/versionaire/version.rb