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'}
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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ ISODuration

Returns a new instance of ISODuration.



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

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.



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

def days
  @days
end

#hoursObject

Returns the value of attribute hours.



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

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes.



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

def minutes
  @minutes
end

#monthsObject

Returns the value of attribute months.



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

def months
  @months
end

#secondsObject

Returns the value of attribute seconds.



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

def seconds
  @seconds
end

#weeksObject

Returns the value of attribute weeks.



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

def weeks
  @weeks
end

#yearsObject

Returns the value of attribute years.



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

def years
  @years
end

Instance Method Details

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#-@Object



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

def -@
  @duration_obj = -@duration_obj
  update_member_variables
end

#==(other) ⇒ Object



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

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

#absObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#normalizeObject



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

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 == 0 && @years == 0
    @weeks += (@days / CONVERSIONS[:d_to_w]).floor
    @days %= CONVERSIONS[:d_to_w]
  end
  if @months > CONVERSIONS[:m_to_y]
    @years += (@months / CONVERSIONS[:m_to_y]).floor
    @months %= CONVERSIONS[:m_to_y]
  end
end

#stringObject



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

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