Class: Lutaml::Model::Type::Date

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/date.rb

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

from_format, #initialize, #initialized?, register_format_to_from_methods, #to_s

Constructor Details

This class inherits a constructor from Lutaml::Model::Type::Value

Class Method Details

.cast(value, _options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lutaml/model/type/date.rb', line 5

def self.cast(value, _options = {})
  return value if value.nil? || Utils.uninitialized?(value)

  case value
  when ::DateTime, ::Time
    value.to_date
  when ::Date
    value
  else
    ::Date.parse(value.to_s)
  end
rescue ArgumentError
  nil
end

.serialize(value) ⇒ Object

xs:date format



21
22
23
24
25
# File 'lib/lutaml/model/type/date.rb', line 21

def self.serialize(value)
  return nil if value.nil?

  value&.iso8601
end

Instance Method Details

#to_yamlObject

This is to handle where Ruby’s YAML safe_load does not handle the Date/Time classes



29
30
31
# File 'lib/lutaml/model/type/date.rb', line 29

def to_yaml
  value&.iso8601.to_s
end