Sha256: d24e3a39b76558e44885e4d23e0c018e2a66c91637ec60a3565b8c0465a80e2f

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

# typed: strict

require 'time'

module Kuby
  module Docker
    class TimestampTag
      extend T::Sig

      FORMAT = T.let('%Y%m%d%H%M%S'.freeze, String)

      sig { params(str: T.nilable(String)).returns(T.nilable(TimestampTag)) }
      def self.try_parse(str)
        return nil unless str

        # The strptime function stops scanning after the pattern has been matched, so
        # we check for all numbers here to prevent things like 20210424165405-assets
        # from being treated as a timestamp tag.
        return nil unless str =~ /\A\d+\z/

        time = begin
          Time.strptime(str, FORMAT)
        rescue ArgumentError
          return nil
        end

        new(time)
      end

      sig { returns(Time) }
      attr_reader :time

      sig { params(time: Time).void }
      def initialize(time)
        @time = T.let(time, Time)
      end

      sig { returns(String) }
      def to_s
        time.strftime(FORMAT)
      end

      sig { params(other: TimestampTag).returns(T.nilable(Integer)) }
      def <=>(other)
        time <=> other.time
      end

      sig { params(other: TimestampTag).returns(T::Boolean) }
      def ==(other)
        time == other.time
      end

      sig { returns(Integer) }
      def hash
        time.hash
      end

      sig { params(other: TimestampTag).returns(T::Boolean) }
      def eql?(other)
        time == other.time
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kuby-core-0.17.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.16.1 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.16.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.15.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.14.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.13.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.12.0 lib/kuby/docker/timestamp_tag.rb