File: epoch.rb

package info (click to toggle)
ruby-version-gem 1.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 268 kB
  • sloc: ruby: 184; makefile: 4
file content (70 lines) | stat: -rwxr-xr-x 1,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require_relative "error"
require_relative "api"

module VersionGem
  # Support for Epoch Semantic Versioning
  # See: https://antfu.me/posts/epoch-semver
  module Epoch
    EPOCH_SIZE = 1_000

    class << self
      def extended(base)
        raise Error, "VERSION must be defined before 'extend #{name}'" unless defined?(base::VERSION)

        base.extend(Api)
        base.extend(OverloadApiForEpoch)
      end
    end

    # Tweak the basic API so it will support Epoch Semantic Versioning
    module OverloadApiForEpoch
      # *** OVERLOAD METHODS FROM API ***
      #
      # The epoch version
      #
      # @return [Integer]
      def epoch
        @epoch ||= _major / EPOCH_SIZE
      end

      # The major version
      #
      # @return [Integer]
      def major
        @major ||= _major % EPOCH_SIZE
      end

      # The version number as a hash
      #
      # @return [Hash]
      def to_h
        @to_h ||= {
          epoch: epoch,
          major: major,
          minor: minor,
          patch: patch,
          pre: pre,
        }
      end

      # NOTE: This is not the same as _to_a, which returns an array of strings
      #
      # The version number as an array of cast values
      # where epoch and major are derived from a single string:
      #   EPOCH * 1000 + MAJOR
      #
      # @return [Array<[Integer, String, NilClass]>]
      def to_a
        @to_a ||= [epoch, major, minor, patch, pre]
      end

      private

      def _major
        @_major ||= _to_a[0].to_i
      end
    end
  end
end