Class: Teems::Models::Duration

Inherits:
Data
  • Object
show all
Defined in:
lib/teems/models/duration.rb

Overview

Immutable duration value object for human-friendly time input

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#secondsObject (readonly)

Returns the value of attribute seconds

Returns:

  • (Object)

    the current value of seconds



8
9
10
# File 'lib/teems/models/duration.rb', line 8

def seconds
  @seconds
end

Class Method Details

.parse(input) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
# File 'lib/teems/models/duration.rb', line 9

def self.parse(input)
  label = input.inspect
  match = DURATION_PATTERN.match(input.to_s.strip)
  raise ArgumentError, "Invalid duration: #{label}" unless match

  total = (match[1].to_i * 3600) + (match[2].to_i * 60)
  raise ArgumentError, "Duration must be greater than zero: #{label}" if total.zero?

  new(seconds: total)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


38
# File 'lib/teems/models/duration.rb', line 38

def empty? = seconds.zero?

#to_expirationObject



27
28
29
# File 'lib/teems/models/duration.rb', line 27

def to_expiration
  (Time.now.utc + seconds).strftime('%Y-%m-%dT%H:%M:%S.0000000Z')
end

#to_iso8601_durationObject



20
21
22
23
24
25
# File 'lib/teems/models/duration.rb', line 20

def to_iso8601_duration
  parts = []
  parts << "#{hours}H" if hours.positive?
  parts << "#{minutes}M" if minutes.positive?
  "PT#{parts.join}"
end

#to_sObject



31
32
33
34
35
36
# File 'lib/teems/models/duration.rb', line 31

def to_s
  parts = []
  parts << "#{hours}h" if hours.positive?
  parts << "#{minutes}m" if minutes.positive?
  parts.join(' ')
end