Class: DecisionAgent::Dmn::Feel::Types::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/dmn/feel/types.rb

Overview

Wrapper for durations (ISO 8601: P1Y2M3DT4H5M6S)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ Duration

Returns a new instance of Duration.



148
149
150
151
152
153
154
155
# File 'lib/decision_agent/dmn/feel/types.rb', line 148

def initialize(years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
  @years = years
  @months = months
  @days = days
  @hours = hours
  @minutes = minutes
  @seconds = seconds
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



146
147
148
# File 'lib/decision_agent/dmn/feel/types.rb', line 146

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



146
147
148
# File 'lib/decision_agent/dmn/feel/types.rb', line 146

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



146
147
148
# File 'lib/decision_agent/dmn/feel/types.rb', line 146

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



146
147
148
# File 'lib/decision_agent/dmn/feel/types.rb', line 146

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



146
147
148
# File 'lib/decision_agent/dmn/feel/types.rb', line 146

def seconds
  @seconds
end

#yearsObject (readonly)

Returns the value of attribute years.



146
147
148
# File 'lib/decision_agent/dmn/feel/types.rb', line 146

def years
  @years
end

Class Method Details

.extract_unit(str, unit) ⇒ Object



213
214
215
216
# File 'lib/decision_agent/dmn/feel/types.rb', line 213

def self.extract_unit(str, unit)
  match = str.match(/(\d+(?:\.\d+)?)#{unit}/)
  match ? match[1].to_f.to_i : 0
end

.parse(iso_string) ⇒ Object

Parse ISO 8601 duration string Examples: P1Y, P1M, P1D, PT1H, PT1M, PT1S, P1Y2M3DT4H5M6S

Raises:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/decision_agent/dmn/feel/types.rb', line 159

def self.parse(iso_string)
  raise FeelTypeError, "Duration must be a string, got #{iso_string.class}" unless iso_string.is_a?(String)
  raise FeelTypeError, "Invalid duration format: must start with 'P'" unless iso_string.start_with?("P")

  # Split on 'T' to separate date and time parts
  parts = iso_string[1..].split("T")
  date_part = parts[0] || ""
  time_part = parts[1] || ""

  duration_attrs = {}

  # Parse date part (Y, M, D)
  duration_attrs[:years] = extract_unit(date_part, "Y")
  duration_attrs[:months] = extract_unit(date_part, "M")
  duration_attrs[:days] = extract_unit(date_part, "D")

  # Parse time part (H, M, S)
  duration_attrs[:hours] = extract_unit(time_part, "H")
  duration_attrs[:minutes] = extract_unit(time_part, "M")
  duration_attrs[:seconds] = extract_unit(time_part, "S")

  new(**duration_attrs)
end

Instance Method Details

#==(other) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/decision_agent/dmn/feel/types.rb', line 198

def ==(other)
  return false unless other.is_a?(Duration)

  @years == other.years &&
    @months == other.months &&
    @days == other.days &&
    @hours == other.hours &&
    @minutes == other.minutes &&
    @seconds == other.seconds
end

#inspectObject



209
210
211
# File 'lib/decision_agent/dmn/feel/types.rb', line 209

def inspect
  "#<Feel::Duration P#{@years}Y#{@months}M#{@days}DT#{@hours}H#{@minutes}M#{@seconds}S>"
end

#to_rubyObject



194
195
196
# File 'lib/decision_agent/dmn/feel/types.rb', line 194

def to_ruby
  to_seconds
end

#to_secondsObject

Convert to total seconds (approximation for date parts)



184
185
186
187
188
189
190
191
192
# File 'lib/decision_agent/dmn/feel/types.rb', line 184

def to_seconds
  total = @seconds
  total += @minutes * 60
  total += @hours * 3600
  total += @days * 86_400
  total += @months * 2_592_000 # 30 days average
  total += @years * 31_536_000 # 365 days
  total
end