Sha256: 0faea7beefe44efe8683f31fe85475333f2ac894e670f3e282f76dffccc0ca94

Contents?: true

Size: 1.28 KB

Versions: 94

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require "active_support/duration"

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
        class Interval < Type::Value # :nodoc:
          def type
            :interval
          end

          def cast_value(value)
            case value
            when ::ActiveSupport::Duration
              value
            when ::String
              begin
                ::ActiveSupport::Duration.parse(value)
              rescue ::ActiveSupport::Duration::ISO8601Parser::ParsingError
                nil
              end
            else
              super
            end
          end

          def serialize(value)
            case value
            when ::ActiveSupport::Duration
              value.iso8601(precision: self.precision)
            when ::Numeric
              # Sometimes operations on Times returns just float number of seconds so we need to handle that.
              # Example: Time.current - (Time.current + 1.hour) # => -3600.000001776 (Float)
              value.seconds.iso8601(precision: self.precision)
            else
              super
            end
          end

          def type_cast_for_schema(value)
            serialize(value).inspect
          end
        end
      end
    end
  end
end

Version data entries

94 entries across 89 versions & 8 rubygems

Version Path
activerecord-7.1.5 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.1.4.2 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.0.8.6 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-6.1.7.10 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-6.1.7.9 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.1.4.1 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.0.8.5 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.1.4 lib/active_record/connection_adapters/postgresql/oid/interval.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/connection_adapters/postgresql/oid/interval.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/connection_adapters/postgresql/oid/interval.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/connection_adapters/postgresql/oid/interval.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/connection_adapters/postgresql/oid/interval.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/connection_adapters/postgresql/oid/interval.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.1.3.4 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.0.8.4 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-6.1.7.8 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.1.3.2 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.1.3.1 lib/active_record/connection_adapters/postgresql/oid/interval.rb
activerecord-7.0.8.1 lib/active_record/connection_adapters/postgresql/oid/interval.rb