Class: Slk::Models::Duration

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

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/slk/models/duration.rb', line 8

def seconds
  @seconds
end

Class Method Details

.from_minutes(minutes) ⇒ Object



19
20
21
# File 'lib/slk/models/duration.rb', line 19

def from_minutes(minutes)
  new(seconds: minutes.to_i * 60)
end

.parse(input) ⇒ Object



10
11
12
13
14
15
# File 'lib/slk/models/duration.rb', line 10

def parse(input)
  return new(seconds: 0) if input.nil? || input.to_s.strip.empty?
  return new(seconds: input.to_i) if input.to_s.match?(/^\d+$/)

  parse_duration_string(input.to_s.downcase, input)
end

.zeroObject



17
# File 'lib/slk/models/duration.rb', line 17

def zero = new(seconds: 0)

Instance Method Details

#+(other) ⇒ Object



64
65
66
# File 'lib/slk/models/duration.rb', line 64

def +(other)
  Duration.new(seconds: seconds + other.seconds)
end

#-(other) ⇒ Object



68
69
70
# File 'lib/slk/models/duration.rb', line 68

def -(other)
  Duration.new(seconds: [seconds - other.seconds, 0].max)
end

#to_expirationObject



52
53
54
55
56
# File 'lib/slk/models/duration.rb', line 52

def to_expiration
  return 0 if zero?

  Time.now.to_i + seconds
end

#to_minutesObject



50
# File 'lib/slk/models/duration.rb', line 50

def to_minutes = (seconds / 60.0).ceil

#to_sObject



58
59
60
61
62
# File 'lib/slk/models/duration.rb', line 58

def to_s
  return '' if zero?

  format_duration
end

#zero?Boolean

Returns:

  • (Boolean)


48
# File 'lib/slk/models/duration.rb', line 48

def zero? = seconds.zero?