Class: Lutaml::Model::Type::Duration

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

Overview

ISO 8601 Duration type

Handles durations in the format: P[n]Y[n]M[n]DT[n]H[n]MS Examples: “P1Y2M3D”, “PT4H5M6S”, “P1Y2M3DT4H5M6S”

Examples:

Using duration type

attribute :processing_time, :duration

Constant Summary

Constants inherited from Value

Value::EMPTY_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

format_type_serializer_for, from_format, #initialized?, register_format_to_from_methods, register_format_type_serializer

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

Constructor Details

#initialize(value) ⇒ Duration

Returns a new instance of Duration.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lutaml/model/type/duration.rb', line 16

def initialize(value)
  if value.is_a?(Duration)
    @years = value.years
    @months = value.months
    @days = value.days
    @hours = value.hours
    @minutes = value.minutes
    @seconds = value.seconds
    @value = value.to_s
  else
    @value = self.class.cast(value)
    parse_duration(@value) if @value
  end
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



14
15
16
# File 'lib/lutaml/model/type/duration.rb', line 14

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



14
15
16
# File 'lib/lutaml/model/type/duration.rb', line 14

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



14
15
16
# File 'lib/lutaml/model/type/duration.rb', line 14

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



14
15
16
# File 'lib/lutaml/model/type/duration.rb', line 14

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



14
15
16
# File 'lib/lutaml/model/type/duration.rb', line 14

def seconds
  @seconds
end

#yearsObject (readonly)

Returns the value of attribute years.



14
15
16
# File 'lib/lutaml/model/type/duration.rb', line 14

def years
  @years
end

Class Method Details

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



31
32
33
34
35
36
37
38
# File 'lib/lutaml/model/type/duration.rb', line 31

def self.cast(value, _options = {})
  return nil if value.nil?
  return value if Utils.uninitialized?(value)
  return value.to_s if value.is_a?(Duration)
  return value if value.is_a?(::String) && valid_duration?(value)

  value.to_s
end

.default_xsd_typeString

XSD type for Duration

Returns:



50
51
52
# File 'lib/lutaml/model/type/duration.rb', line 50

def self.default_xsd_type
  "xs:duration"
end

.serialize(value) ⇒ Object



40
41
42
43
44
45
# File 'lib/lutaml/model/type/duration.rb', line 40

def self.serialize(value)
  return nil if value.nil?
  return value.to_s if value.is_a?(Duration)

  value.to_s
end

.valid_duration?(str) ⇒ Boolean

Returns:



60
61
62
63
# File 'lib/lutaml/model/type/duration.rb', line 60

def self.valid_duration?(str)
  # Basic ISO 8601 duration validation
  str.match?(/^P(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?$/)
end

Instance Method Details

#to_sObject



54
55
56
# File 'lib/lutaml/model/type/duration.rb', line 54

def to_s
  @value
end