Sha256: 002c9627acdddeb62bd6807572676937460458d12614f24c1bd1a650b34f0345

Contents?: true

Size: 1.87 KB

Versions: 48

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/object/blank"
require "active_support/core_ext/hash/transform_values"

module ActiveSupport
  class Duration
    # Serializes duration to string according to ISO 8601 Duration format.
    class ISO8601Serializer # :nodoc:
      def initialize(duration, precision: nil)
        @duration = duration
        @precision = precision
      end

      # Builds and returns output string.
      def serialize
        parts, sign = normalize
        return "PT0S".freeze if parts.empty?

        output = "P".dup
        output << "#{parts[:years]}Y"   if parts.key?(:years)
        output << "#{parts[:months]}M"  if parts.key?(:months)
        output << "#{parts[:weeks]}W"   if parts.key?(:weeks)
        output << "#{parts[:days]}D"    if parts.key?(:days)
        time = "".dup
        time << "#{parts[:hours]}H"     if parts.key?(:hours)
        time << "#{parts[:minutes]}M"   if parts.key?(:minutes)
        if parts.key?(:seconds)
          time << "#{sprintf(@precision ? "%0.0#{@precision}f" : '%g', parts[:seconds])}S"
        end
        output << "T#{time}" unless time.empty?
        "#{sign}#{output}"
      end

      private

        # Return pair of duration's parts and whole duration sign.
        # Parts are summarized (as they can become repetitive due to addition, etc).
        # Zero parts are removed as not significant.
        # If all parts are negative it will negate all of them and return minus as a sign.
        def normalize
          parts = @duration.parts.each_with_object(Hash.new(0)) do |(k, v), p|
            p[k] += v  unless v.zero?
          end
          # If all parts are negative - let's make a negative duration
          sign = ""
          if parts.values.all? { |v| v < 0 }
            sign = "-"
            parts.transform_values!(&:-@)
          end
          [parts, sign]
        end
    end
  end
end

Version data entries

48 entries across 48 versions & 7 rubygems

Version Path
activesupport-5.2.8.1 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.8 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.7.1 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.7 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.6.3 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.6.2 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.6.1 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.6 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.6 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.5 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.5 lib/active_support/duration/iso8601_serializer.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.1/lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.4 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.3 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.2 lib/active_support/duration/iso8601_serializer.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.4.1/lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.1 lib/active_support/duration/iso8601_serializer.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4 lib/active_support/duration/iso8601_serializer.rb
activesupport-5.2.4.rc1 lib/active_support/duration/iso8601_serializer.rb