Class: JLDrill::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/jldrill/model/Duration.rb

Overview

Holds a duration in seconds. Note that this is an integer value and can’t be used for small measurements. A value less than zero means that it is an invalid duration. Also since it is a signed int you should refrain from using values larger than about 68 years

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds = -1)) ⇒ Duration

Returns a new instance of Duration.



11
12
13
# File 'lib/jldrill/model/Duration.rb', line 11

def initialize(seconds = -1)
    @seconds = seconds
end

Class Method Details

.parse(string) ⇒ Object

Takes an integer as a string and returns a duration.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jldrill/model/Duration.rb', line 42

def Duration.parse(string)
    duration = string.to_i
    # When to_i fails, it returns 0.  We need to differential
    # between that and a real 0.
    if (duration != 0) || string.start_with?("0")
        return Duration.new(duration)
    else
        # Return an invalid Duration
        return Duration.new()
    end
end

Instance Method Details

#assign(duration) ⇒ Object

assigns this duration to be the same as the one passed in



16
17
18
# File 'lib/jldrill/model/Duration.rb', line 16

def assign(duration)
    @seconds = duration.seconds
end

#daysObject

Returns the duration in days as a floating point number



31
32
33
# File 'lib/jldrill/model/Duration.rb', line 31

def days
    return @seconds.to_f / 60.0 / 60.0 / 24.0
end

#days=(days) ⇒ Object

Sets the duration to be the number of days passed in. If this happens to end up as a fraction of seconds, the result is truncated.



37
38
39
# File 'lib/jldrill/model/Duration.rb', line 37

def days=(days)
    @seconds = (days * 24 * 60 * 60).to_i
end

#secondsObject

Returns the duration in seconds



21
22
23
# File 'lib/jldrill/model/Duration.rb', line 21

def seconds
    return @seconds
end

#seconds=(seconds) ⇒ Object

Sets the duration to be equal to the number of seconds passed in



26
27
28
# File 'lib/jldrill/model/Duration.rb', line 26

def seconds=(seconds)
    @seconds = seconds
end

#to_sObject

Returns the duration as a string representing the number of seconds



60
61
62
# File 'lib/jldrill/model/Duration.rb', line 60

def to_s
    return @seconds.to_s
end

#valid?Boolean

Returns false if the duration isn’t valid

Returns:

  • (Boolean)


55
56
57
# File 'lib/jldrill/model/Duration.rb', line 55

def valid?
    return (@seconds >= 0)
end