Class: Rails::GraphQL::Type::Scalar::TimeScalar

Inherits:
FloatScalar show all
Defined in:
lib/rails/graphql/type/scalar/time_scalar.rb

Overview

Uses as a float extension in order to transmit times (hours, minutes, and seconds) as a numeric representation of seconds and milliseconds.

Constant Summary collapse

EPOCH =
Time.utc(2000, 1, 1)

Constants inherited from Rails::GraphQL::Type

KINDS

Class Method Summary collapse

Methods inherited from Rails::GraphQL::Type::Scalar

inspect, to_json

Methods inherited from Rails::GraphQL::Type

=~, base_type, create!, decorate, find_by_gid, gid_base_class, input_type?, kind, kind_enum, leaf_type?, operational?, output_type?, to_gql_backtrace

Methods included from Helpers::WithDirectives

#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?, #validate!

Methods included from Helpers::WithGlobalID

#to_gid_param, #to_global_id

Methods included from Helpers::Registerable

#aliases, extended, #inherited, #register!, #registered?

Class Method Details

.as_json(value) ⇒ Object



31
32
33
# File 'lib/rails/graphql/type/scalar/time_scalar.rb', line 31

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

.deserialize(value) ⇒ Object



35
36
37
# File 'lib/rails/graphql/type/scalar/time_scalar.rb', line 35

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

.valid_input?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rails/graphql/type/scalar/time_scalar.rb', line 23

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

.valid_output?(value) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rails/graphql/type/scalar/time_scalar.rb', line 27

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