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.
- #==(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.
30 31 32 33 34 |
# File 'lib/iso8601/duration.rb', line 30 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.
40 41 42 |
# File 'lib/iso8601/duration.rb', line 40 def atoms @atoms end |
#pattern ⇒ String (readonly) Also known as: to_s
Returns The string representation of the duration.
44 45 46 |
# File 'lib/iso8601/duration.rb', line 44 def pattern @pattern end |
#sign ⇒ Integer (readonly)
The Integer representation of the duration sign.
93 94 95 |
# File 'lib/iso8601/duration.rb', line 93 def sign @sign end |
Instance Method Details
#+(other) ⇒ ISO8601::Duration
Addition
107 108 109 |
# File 'lib/iso8601/duration.rb', line 107 def +(other) seconds_to_iso(to_seconds + fetch_seconds(other)) end |
#-(other) ⇒ ISO8601::Duration
Substraction
117 118 119 |
# File 'lib/iso8601/duration.rb', line 117 def -(other) seconds_to_iso(to_seconds - fetch_seconds(other)) end |
#==(other) ⇒ Boolean
125 126 127 |
# File 'lib/iso8601/duration.rb', line 125 def ==(other) (to_seconds == fetch_seconds(other)) end |
#abs ⇒ ISO8601::Duration
Returns The absolute representation of the duration.
97 98 99 |
# File 'lib/iso8601/duration.rb', line 97 def abs self.class.new(pattern.sub(/^[-+]/, '')) end |
#days ⇒ ISO8601::Days
Returns The days of the duration.
67 68 69 |
# File 'lib/iso8601/duration.rb', line 67 def days ISO8601::Days.new(atoms[:days]) end |
#eql?(other) ⇒ Boolean
133 134 135 |
# File 'lib/iso8601/duration.rb', line 133 def eql?(other) (hash == other.hash) end |
#hash ⇒ Fixnum
139 140 141 |
# File 'lib/iso8601/duration.rb', line 139 def hash [atoms.values, self.class].hash end |
#hours ⇒ ISO8601::Hours
Returns The hours of the duration.
73 74 75 |
# File 'lib/iso8601/duration.rb', line 73 def hours ISO8601::Hours.new(atoms[:hours]) end |
#minutes ⇒ ISO8601::Minutes
Returns The minutes of the duration.
79 80 81 |
# File 'lib/iso8601/duration.rb', line 79 def minutes ISO8601::Minutes.new(atoms[:minutes]) end |
#months ⇒ ISO8601::Months
Returns The months of the duration.
55 56 57 |
# File 'lib/iso8601/duration.rb', line 55 def months ISO8601::Months.new(atoms[:months]) end |
#seconds ⇒ ISO8601::Seconds
Returns The seconds of the duration.
85 86 87 |
# File 'lib/iso8601/duration.rb', line 85 def seconds ISO8601::Seconds.new(atoms[:seconds]) end |
#to_pattern(original) ⇒ String
Converts original input into a valid ISO 8601 duration pattern.
147 148 149 |
# File 'lib/iso8601/duration.rb', line 147 def to_pattern(original) (original.is_a? Numeric) ? "PT#{original}S" : original end |
#to_seconds(base = nil) ⇒ Numeric
Returns The duration in seconds.
156 157 158 159 160 |
# File 'lib/iso8601/duration.rb', line 156 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.
61 62 63 |
# File 'lib/iso8601/duration.rb', line 61 def weeks ISO8601::Weeks.new(atoms[:weeks]) end |
#years ⇒ ISO8601::Years
Returns The years of the duration.
49 50 51 |
# File 'lib/iso8601/duration.rb', line 49 def years ISO8601::Years.new(atoms[:years]) end |