Class: Shale::Type::Time

Inherits:
Value
  • Object
show all
Defined in:
lib/shale/type/time.rb

Overview

Cast value to Time

Class Method Summary collapse

Methods inherited from Value

as_hash, as_toml, as_xml, of_csv, of_hash, of_json, of_toml, of_xml, of_yaml

Class Method Details

.as_csv(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use ISO 8601 format in CSV document

Parameters:

Returns:



57
58
59
# File 'lib/shale/type/time.rb', line 57

def self.as_csv(value, **)
  value&.iso8601
end

.as_json(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use ISO 8601 format in JSON document

Parameters:

Returns:



35
36
37
# File 'lib/shale/type/time.rb', line 35

def self.as_json(value, **)
  value&.iso8601
end

.as_xml_value(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use ISO 8601 format in XML document

Parameters:

  • value (Time)

    Value to convert to XML

Returns:



68
69
70
# File 'lib/shale/type/time.rb', line 68

def self.as_xml_value(value)
  value&.iso8601
end

.as_yaml(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use ISO 8601 format in YAML document

Parameters:

Returns:



46
47
48
# File 'lib/shale/type/time.rb', line 46

def self.as_yaml(value, **)
  value&.iso8601
end

.cast(value) ⇒ Time?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (any)

    Value to cast

Returns:



17
18
19
20
21
22
23
24
25
26
# File 'lib/shale/type/time.rb', line 17

def self.cast(value)
  if value.is_a?(::String)
    return if value.empty?
    ::Time.parse(value)
  elsif value.respond_to?(:to_time)
    value.to_time
  else
    value
  end
end