Class: Lutaml::Model::Type::TimeWithoutDate

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/time_without_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

TODO: we probably want to do something like this because using Time.parse will set the date to today.

time = ::Time.parse(value.to_s) ::Time.new(1, 1, 1, time.hour, time.min, time.sec)



13
14
15
16
17
18
19
20
21
22
# File 'lib/lutaml/model/type/time_without_date.rb', line 13

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

  case value
  when ::Time then value
  else ::Time.parse(value.to_s)
  end
rescue ArgumentError
  nil
end

.serialize(value) ⇒ Object



24
25
26
27
28
29
# File 'lib/lutaml/model/type/time_without_date.rb', line 24

def self.serialize(value)
  return value if value.nil? || Utils.uninitialized?(value)

  value = cast(value)
  value.strftime("%H:%M:%S") # Format as HH:MM:SS
end

Instance Method Details

#to_json(*_args) ⇒ Object



36
37
38
# File 'lib/lutaml/model/type/time_without_date.rb', line 36

def to_json(*_args)
  self.class.serialize(value)
end

#to_tomlObject



44
45
46
# File 'lib/lutaml/model/type/time_without_date.rb', line 44

def to_toml
  value.strftime("%H:%M:%S.%L") # Include milliseconds for TOML
end

#to_xmlObject

Instance methods for format-specific serialization



32
33
34
# File 'lib/lutaml/model/type/time_without_date.rb', line 32

def to_xml
  self.class.serialize(value)
end

#to_yamlObject



40
41
42
# File 'lib/lutaml/model/type/time_without_date.rb', line 40

def to_yaml
  self.class.serialize(value)
end