Sha256: b095830ccb860496d7603c87386f3d6bf22e785c6529e1c329c64a70d08e52f3

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Rails # :nodoc:
  module GraphQL # :nodoc:
    class Type # :nodoc:
      # Uses as a float extension in order to transmit times (hours, minutes,
      # and seconds) as a numeric representation of seconds and milliseconds.
      class Scalar::TimeScalar < Scalar::FloatScalar
        EPOCH = Time.utc(2000, 1, 1)

        desc <<~MSG
          The Time scalar type that represents a distance in time using hours,
          minutes, seconds, and miliseconds.
        MSG

        # A +base_object+ helps to identify what methods are actually available
        # to work as resolvers
        class_attribute :precision, instance_writer: false, default: 6

        class << self
          def valid_input?(value)
            value.match?(/\d+:\d\d(:\d\d(\.\d+)?)?/)
          end

          def valid_output?(value)
            value.respond_to?(:to_time)
          end

          def as_json(value)
            value.to_time.strftime(format('%%T.%%%dN', precision))
          end

          def deserialize(value)
            "#{EPOCH.to_date.iso8601} #{value} UTC".to_time
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-graphql-0.2.1 lib/rails/graphql/type/scalar/time_scalar.rb
rails-graphql-0.2.0 lib/rails/graphql/type/scalar/time_scalar.rb
rails-graphql-0.1.3 lib/rails/graphql/type/scalar/time_scalar.rb
rails-graphql-0.1.2 lib/rails/graphql/type/scalar/time_scalar.rb
rails-graphql-0.1.1 lib/rails/graphql/type/scalar/time_scalar.rb
rails-graphql-0.1.0 lib/rails/graphql/type/scalar/time_scalar.rb