Class: MicrosoftKiotaAbstractions::ISODuration

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft_kiota_abstractions/serialization/iso_duration.rb

Overview

Wrapper Class for ISO8601::Duration Integer support for :years, :months, :weeks, :days, :hours, :minutes, :seconds Initialize with a hash of symbols to integers eg { :years => 3, :days => 4, seconds: => 2} or with an ISO8601 formated string eg "PT3H12M5S".

Constant Summary collapse

UNITS =
{ years: 'Y',
months: 'M',
weeks: 'W',
days: 'D',
hours: 'H',
minutes: 'M',
seconds: 'S' }.freeze
CONVERSIONS =
{
  ms_to_s: 1000,
  s_to_m: 60,
  m_to_h: 60,
  h_to_d: 24,
  d_to_w: 7,
  m_to_y: 12
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ ISODuration

Returns a new instance of ISODuration.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 29

def initialize(input)
  if input.is_a? String
    @duration_obj = ISO8601::Duration.new(input)
  elsif input.is_a? Hash
    @duration_obj = parse_hash(input)
  else
    raise StandardError, 'Must provide initialize ISODuration by providing a hash or an ISO8601-formatted string.'
  end
  update_member_variables
  normalize
end

Instance Attribute Details

#daysObject

Returns the value of attribute days.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def days
  @days
end

#hoursObject

Returns the value of attribute hours.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def minutes
  @minutes
end

#monthsObject

Returns the value of attribute months.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def months
  @months
end

#secondsObject

Returns the value of attribute seconds.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def seconds
  @seconds
end

#weeksObject

Returns the value of attribute weeks.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def weeks
  @weeks
end

#yearsObject

Returns the value of attribute years.



11
12
13
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 11

def years
  @years
end

Instance Method Details

#+(other) ⇒ Object



147
148
149
150
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 147

def +(other)
  new_obj = duration_obj + other.duration_obj
  MicrosoftKiotaAbstractions::ISODuration.new(dur_obj_to_hash(new_obj))
end

#-(other) ⇒ Object



152
153
154
155
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 152

def -(other)
  new_obj = duration_obj - other.duration_obj
  MicrosoftKiotaAbstractions::ISODuration.new(dur_obj_to_hash(new_obj))
end

#-@Object



161
162
163
164
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 161

def -@
  @duration_obj = -@duration_obj
  update_member_variables
end

#==(other) ⇒ Object



157
158
159
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 157

def ==(other)
  @duration_obj == other.duration_obj
end

#absObject



141
142
143
144
145
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 141

def abs
  @duration_obj = @duration_obj.abs
  update_member_variables
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 166

def eql?(other)
  @duration_obj == other.duration_obj
end

#normalizeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 55

def normalize
  if @seconds >= CONVERSIONS[:s_to_m]
    @minutes += (@seconds / CONVERSIONS[:s_to_m]).floor
    @seconds %= CONVERSIONS[:s_to_m]
  end
  if @minutes >= CONVERSIONS[:m_to_h]
    @hours += (@minutes / CONVERSIONS[:m_to_h]).floor
    @minutes %= CONVERSIONS[:m_to_h]
  end
  if @hours >= CONVERSIONS[:h_to_d]
    @days += (@hours / CONVERSIONS[:h_to_d]).floor
    @hours %= CONVERSIONS[:h_to_d]
  end
  if @days >= CONVERSIONS[:d_to_w] && @months.zero? && @years.zero?
    @weeks += (@days / CONVERSIONS[:d_to_w]).floor
    @days %= CONVERSIONS[:d_to_w]
  end
  return unless @months > CONVERSIONS[:m_to_y]

  @years += (@months / CONVERSIONS[:m_to_y]).floor
  @months %= CONVERSIONS[:m_to_y]
end

#stringObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/microsoft_kiota_abstractions/serialization/iso_duration.rb', line 41

def string
  input = { seconds: @seconds, minutes: @minutes, hours: @hours,
            days: @days, weeks: @weeks, months: @months,
            years: @years }
  iso_str = 'P'
  UNITS.each do |unit, abrev|
    iso_str += input[unit].to_s + abrev unless input[unit].zero?
    iso_str += 'T' if unit == :days
  end
  iso_str = iso_str.strip
  iso_str = iso_str.chomp('T') if iso_str[-1].eql? 'T'
  iso_str
end