Sha256: 876046491c1d8142a338240f834da3bd233e82b42c9398a90e865b9819d84dc9

Contents?: true

Size: 893 Bytes

Versions: 28

Compression:

Stored size: 893 Bytes

Contents

# frozen_string_literal: true

class Code
  class Object
    class Duration < Object
      def initialize(*args, **_kargs, &_block)
        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

28 entries across 28 versions & 1 rubygems

Version Path
code-ruby-1.0.0 lib/code/object/duration.rb
code-ruby-0.15.16 lib/code/object/duration.rb
code-ruby-0.15.15 lib/code/object/duration.rb
code-ruby-0.15.14 lib/code/object/duration.rb
code-ruby-0.15.13 lib/code/object/duration.rb
code-ruby-0.15.12 lib/code/object/duration.rb
code-ruby-0.15.11 lib/code/object/duration.rb
code-ruby-0.15.10 lib/code/object/duration.rb
code-ruby-0.15.9 lib/code/object/duration.rb
code-ruby-0.15.8 lib/code/object/duration.rb
code-ruby-0.15.7 lib/code/object/duration.rb
code-ruby-0.15.6 lib/code/object/duration.rb
code-ruby-0.15.5 lib/code/object/duration.rb
code-ruby-0.15.4 lib/code/object/duration.rb
code-ruby-0.15.3 lib/code/object/duration.rb
code-ruby-0.15.2 lib/code/object/duration.rb
code-ruby-0.15.0 lib/code/object/duration.rb
code-ruby-0.14.9 lib/code/object/duration.rb
code-ruby-0.14.8 lib/code/object/duration.rb
code-ruby-0.14.6 lib/code/object/duration.rb