Class: Dse::Graph::Duration
- Inherits:
-
Object
- Object
- Dse::Graph::Duration
- 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
-
#days ⇒ Integer
Days in duration of time.
-
#hours ⇒ Integer
Hours in duration of time.
-
#minutes ⇒ Integer
Minutes in duration of time.
-
#seconds ⇒ Float
Seconds in duration of time.
Class Method Summary collapse
-
.parse(duration) ⇒ Object
Parse a duration string from DSE Graph and construct a Duration object.
Instance Method Summary collapse
-
#as_seconds ⇒ Float
This Duration object converted to seconds.
-
#initialize(days, hours, minutes, seconds) ⇒ Duration
constructor
Create a Duration object.
-
#to_s ⇒ String
A string formatted as
PnDTnHnMn.nS
, wheren
is a number that goes with the character code following it.
Constructor Details
#initialize(days, hours, minutes, seconds) ⇒ Duration
Create a Dse::Graph::Duration object. All arguments are internally coerced to desired types.
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
#days ⇒ Integer
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.
19 20 21 |
# File 'lib/dse/graph/duration.rb', line 19 def days @days end |
#hours ⇒ Integer
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.
24 25 26 |
# File 'lib/dse/graph/duration.rb', line 24 def hours @hours end |
#minutes ⇒ Integer
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.
29 30 31 |
# File 'lib/dse/graph/duration.rb', line 29 def minutes @minutes end |
#seconds ⇒ Float
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.
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
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_seconds ⇒ Float
Returns this Dse::Graph::Duration object converted to seconds.
105 106 107 |
# File 'lib/dse/graph/duration.rb', line 105 def as_seconds @seconds + @minutes * 60 + @hours * 3600 + @days * 86400 end |
#to_s ⇒ String
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
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 |