Class: ISO8601::Duration
- Inherits:
-
Object
- Object
- ISO8601::Duration
- Defined in:
- lib/iso8601/duration.rb
Overview
A duration representation. When no base is provided, all atoms use an average factor to compute the amount of seconds.
Instance Attribute Summary collapse
-
#atoms ⇒ Hash<Float>
readonly
Raw atoms result of parsing the given pattern.
-
#pattern ⇒ String
(also: #to_s)
readonly
The string representation of the duration.
-
#sign ⇒ Integer
readonly
The Integer representation of the duration sign.
Instance Method Summary collapse
-
#+(other) ⇒ ISO8601::Duration
Addition.
-
#-(other) ⇒ ISO8601::Duration
Substraction.
- #-@ ⇒ ISO8601::Duration
- #==(other) ⇒ Boolean
-
#abs ⇒ ISO8601::Duration
The absolute representation of the duration.
-
#days ⇒ ISO8601::Days
The days of the duration.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Fixnum
-
#hours ⇒ ISO8601::Hours
The hours of the duration.
-
#initialize(input) ⇒ Duration
constructor
A new instance of Duration.
-
#minutes ⇒ ISO8601::Minutes
The minutes of the duration.
-
#months ⇒ ISO8601::Months
The months of the duration.
-
#seconds ⇒ ISO8601::Seconds
The seconds of the duration.
-
#to_pattern(original) ⇒ String
Converts original input into a valid ISO 8601 duration pattern.
-
#to_seconds(base = nil) ⇒ Numeric
The duration in seconds.
-
#weeks ⇒ ISO8601::Weeks
The weeks of the duration.
-
#years ⇒ ISO8601::Years
The years of the duration.
Constructor Details
#initialize(input) ⇒ Duration
Returns a new instance of Duration.
29 30 31 32 33 |
# File 'lib/iso8601/duration.rb', line 29 def initialize(input) @original = input @pattern = to_pattern(input) @atoms = atomize(@pattern) end |
Instance Attribute Details
#atoms ⇒ Hash<Float> (readonly)
Raw atoms result of parsing the given pattern.
39 40 41 |
# File 'lib/iso8601/duration.rb', line 39 def atoms @atoms end |
#pattern ⇒ String (readonly) Also known as: to_s
Returns The string representation of the duration.
43 44 45 |
# File 'lib/iso8601/duration.rb', line 43 def pattern @pattern end |
#sign ⇒ Integer (readonly)
The Integer representation of the duration sign.
92 93 94 |
# File 'lib/iso8601/duration.rb', line 92 def sign @sign end |
Instance Method Details
#+(other) ⇒ ISO8601::Duration
Addition
106 107 108 |
# File 'lib/iso8601/duration.rb', line 106 def +(other) seconds_to_iso(to_seconds + fetch_seconds(other)) end |
#-(other) ⇒ ISO8601::Duration
Substraction
116 117 118 |
# File 'lib/iso8601/duration.rb', line 116 def -(other) seconds_to_iso(to_seconds - fetch_seconds(other)) end |
#-@ ⇒ ISO8601::Duration
131 132 133 |
# File 'lib/iso8601/duration.rb', line 131 def -@ seconds_to_iso(-to_seconds) end |
#==(other) ⇒ Boolean
124 125 126 |
# File 'lib/iso8601/duration.rb', line 124 def ==(other) (to_seconds == fetch_seconds(other)) end |
#abs ⇒ ISO8601::Duration
Returns The absolute representation of the duration.
96 97 98 |
# File 'lib/iso8601/duration.rb', line 96 def abs self.class.new(pattern.sub(/^[-+]/, '')) end |
#days ⇒ ISO8601::Days
Returns The days of the duration.
66 67 68 |
# File 'lib/iso8601/duration.rb', line 66 def days ISO8601::Days.new(atoms[:days]) end |
#eql?(other) ⇒ Boolean
139 140 141 |
# File 'lib/iso8601/duration.rb', line 139 def eql?(other) (hash == other.hash) end |
#hash ⇒ Fixnum
145 146 147 |
# File 'lib/iso8601/duration.rb', line 145 def hash [atoms.values, self.class].hash end |
#hours ⇒ ISO8601::Hours
Returns The hours of the duration.
72 73 74 |
# File 'lib/iso8601/duration.rb', line 72 def hours ISO8601::Hours.new(atoms[:hours]) end |
#minutes ⇒ ISO8601::Minutes
Returns The minutes of the duration.
78 79 80 |
# File 'lib/iso8601/duration.rb', line 78 def minutes ISO8601::Minutes.new(atoms[:minutes]) end |
#months ⇒ ISO8601::Months
Returns The months of the duration.
54 55 56 |
# File 'lib/iso8601/duration.rb', line 54 def months ISO8601::Months.new(atoms[:months]) end |
#seconds ⇒ ISO8601::Seconds
Returns The seconds of the duration.
84 85 86 |
# File 'lib/iso8601/duration.rb', line 84 def seconds ISO8601::Seconds.new(atoms[:seconds]) end |
#to_pattern(original) ⇒ String
Converts original input into a valid ISO 8601 duration pattern.
153 154 155 156 157 158 159 |
# File 'lib/iso8601/duration.rb', line 153 def to_pattern(original) if original.is_a? Numeric "#{original < 0 ? '-' : ''}PT#{original.abs}S" else original end end |
#to_seconds(base = nil) ⇒ Numeric
Returns The duration in seconds.
166 167 168 169 170 |
# File 'lib/iso8601/duration.rb', line 166 def to_seconds(base = nil) rest = [weeks, days, hours, minutes, seconds].map(&:to_seconds) years.to_seconds(base) + months_to_seconds(base) + rest.reduce(&:+) end |
#weeks ⇒ ISO8601::Weeks
Returns The weeks of the duration.
60 61 62 |
# File 'lib/iso8601/duration.rb', line 60 def weeks ISO8601::Weeks.new(atoms[:weeks]) end |
#years ⇒ ISO8601::Years
Returns The years of the duration.
48 49 50 |
# File 'lib/iso8601/duration.rb', line 48 def years ISO8601::Years.new(atoms[:years]) end |