Sha256: 5ca471050a9728667e3be400e5f62d4e0d1abf1e16169c459907a28d05305a78

Contents?: true

Size: 887 Bytes

Versions: 3

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true

class Code
  class Object
    class Duration < Object
      def initialize(*args, **_kargs, &)
        raw = args.first || 0.seconds
        raw = raw.raw if raw.is_an?(Object)
        raw = raw.iso8601 if raw.is_an?(::ActiveSupport::Duration)
        @raw = ::ActiveSupport::Duration.parse(raw.to_s)
      rescue ::ActiveSupport::Duration::ISO8601Parser::ParsingError
        raise Error, "#{raw.inspect} is not a valid duration"
      end

      def call(**args)
        operator = args.fetch(:operator, nil)

        case operator.to_s
        when "ago"
          sig(args)
          code_ago
        when "from_now"
          sig(args)
          code_from_now
        else
          super
        end
      end

      def code_ago
        Time.new(raw.ago)
      end

      def code_from_now
        Time.new(raw.from_now)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
code-ruby-1.1.3 lib/code/object/duration.rb
code-ruby-1.1.1 lib/code/object/duration.rb
code-ruby-1.1.0 lib/code/object/duration.rb