Class: Dse::Graph::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/dse/graph/duration.rb

Overview

Represents a duration of time, corresponding to the Duration datatype in DSE Graph. In DSE Graph, this type is represented by the Java 8 Duration type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(days, hours, minutes, seconds) ⇒ Duration

Create a Dse::Graph::Duration object. All arguments are internally coerced to desired types.

Parameters:

  • days (Integer)

    number of days in the time-frame. May be negative.

  • hours (Integer)

    number of hours in the time-frame. May be negative.

  • minutes (Integer)

    number of minutes in the time-frame. May be negative.

  • seconds (Float)

    number of seconds in the time-frame. May be negative.



46
47
48
49
50
51
# File 'lib/dse/graph/duration.rb', line 46

def initialize(days, hours, minutes, seconds)
  @days = days.to_i
  @hours = hours.to_i
  @minutes = minutes.to_i
  @seconds = seconds.to_f
end

Instance Attribute Details

#daysInteger

Days in duration of time. May be negative. Is internally coerced to an integer, so a value being assigned need not be an Integer itself.

Returns:

  • (Integer)

    days in duration of time.



19
20
21
# File 'lib/dse/graph/duration.rb', line 19

def days
  @days
end

#hoursInteger

Hours in duration of time. May be negative. Is internally coerced to an integer, so a value being assigned need not be an Integer itself.

Returns:

  • (Integer)

    hours in duration of time.



24
25
26
# File 'lib/dse/graph/duration.rb', line 24

def hours
  @hours
end

#minutesInteger

Minutes in duration of time. May be negative. Is internally coerced to an integer, so a value being assigned need not be an Integer itself.

Returns:

  • (Integer)

    minutes in duration of time.



29
30
31
# File 'lib/dse/graph/duration.rb', line 29

def minutes
  @minutes
end

#secondsFloat

Seconds in duration of time. May be negative. Is internally coerced to an float, so a value being assigned need not be an Float itself.

Returns:

  • (Float)

    seconds in duration of time.



34
35
36
# File 'lib/dse/graph/duration.rb', line 34

def seconds
  @seconds
end

Class Method Details

.parse(duration) ⇒ Object

Parse a duration string from DSE Graph and construct a Dse::Graph::Duration object

Parameters:

  • duration (String)

    duration string from DSE Graph.

Raises:

  • (ArgumentError)

    if the duration string fails to parse.



72
73
74
75
76
77
78
# File 'lib/dse/graph/duration.rb', line 72

def self.parse(duration)
  parse_result = PAT.match(duration.to_s)
  raise(ArgumentError,
        "Failed to parse '#{duration}': expected format PnDTnHnMn.nS with integer n" \
        ' and optionally missing duration components') unless parse_result
  Duration.new(parse_result[:days], parse_result[:hours], parse_result[:minutes], parse_result[:seconds])
end

Instance Method Details

#as_secondsFloat

Returns this Dse::Graph::Duration object converted to seconds.

Returns:



105
106
107
# File 'lib/dse/graph/duration.rb', line 105

def as_seconds
  @seconds + @minutes * 60 + @hours * 3600 + @days * 86400
end

#to_sString

A string formatted as PnDTnHnMn.nS, where n is a number that goes with the character code following it.

D - days
H - hours
M - minutes
S - seconds

Examples:

a duration of 1 day, 2 hours, 3 minutes, 4.5 seconds

P1DT2H3M4.5S

Returns:

See Also:



93
94
95
96
# File 'lib/dse/graph/duration.rb', line 93

def to_s
  # Construct a string of the form PnDTnHnMn.nS
  "P#{@days}DT#{@hours}H#{@minutes}M#{@seconds}S"
end