Class: Lutaml::Model::Type::Duration
- 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”
Constant Summary
Constants inherited from Value
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#years ⇒ Object
readonly
Returns the value of attribute years.
Attributes inherited from Value
Class Method Summary collapse
- .cast(value, _options = {}) ⇒ Object
-
.default_xsd_type ⇒ String
XSD type for Duration.
- .serialize(value) ⇒ Object
- .valid_duration?(str) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(value) ⇒ Duration
constructor
A new instance of Duration.
- #to_s ⇒ Object
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
Methods included from UninitializedClassGuard
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
#days ⇒ Object (readonly)
Returns the value of attribute days.
14 15 16 |
# File 'lib/lutaml/model/type/duration.rb', line 14 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
14 15 16 |
# File 'lib/lutaml/model/type/duration.rb', line 14 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
14 15 16 |
# File 'lib/lutaml/model/type/duration.rb', line 14 def minutes @minutes end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
14 15 16 |
# File 'lib/lutaml/model/type/duration.rb', line 14 def months @months end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
14 15 16 |
# File 'lib/lutaml/model/type/duration.rb', line 14 def seconds @seconds end |
#years ⇒ Object (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, = {}) 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_type ⇒ String
XSD type for Duration
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
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_s ⇒ Object
54 55 56 |
# File 'lib/lutaml/model/type/duration.rb', line 54 def to_s @value end |