Class: ISO8601Basic::Duration
- Inherits:
-
Object
- Object
- ISO8601Basic::Duration
- Defined in:
- lib/iso8601_basic/duration.rb
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
-
#weeks ⇒ Object
readonly
Returns the value of attribute weeks.
-
#years ⇒ Object
readonly
Returns the value of attribute years.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json ⇒ Object
- #blank? ⇒ Boolean
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #format ⇒ Object
- #hash ⇒ Object
-
#initialize(value) ⇒ Duration
constructor
A new instance of Duration.
- #inspect ⇒ Object
- #iso8601 ⇒ Object
- #present? ⇒ Boolean
- #to_a ⇒ Object
- #to_f ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ Duration
Returns a new instance of Duration.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/iso8601_basic/duration.rb', line 5 def initialize(value) match = /^(\+|-)? # Sign P( ( (\d+(?:[,.]\d+)?Y)? # Years (\d+(?:[.,]\d+)?M)? # Months (\d+(?:[.,]\d+)?D)? # Days (T (\d+(?:[.,]\d+)?H)? # Hours (\d+(?:[.,]\d+)?M)? # Minutes (\d+(?:[.,]\d+)?S)? # Seconds )? # Time ) |(\d+(?:[.,]\d+)?W) # Weeks ) # Duration $/x.match(value) raise ArgumentError, 'invalid duration' unless match @sign = (match[1] == '-' ? -1 : 1) { years: 4, months: 5, weeks: 11, days: 6, hours: 8, minutes: 9, seconds: 10 }.each do |key, index| instance_variable_set "@#{key}", match[index] ? match[index].chop.to_f * @sign : 0 end end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def minutes @minutes end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def months @months end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def seconds @seconds end |
#sign ⇒ Object (readonly)
Returns the value of attribute sign.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def sign @sign end |
#weeks ⇒ Object (readonly)
Returns the value of attribute weeks.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def weeks @weeks end |
#years ⇒ Object (readonly)
Returns the value of attribute years.
3 4 5 |
# File 'lib/iso8601_basic/duration.rb', line 3 def years @years end |
Instance Method Details
#==(other) ⇒ Object
62 63 64 |
# File 'lib/iso8601_basic/duration.rb', line 62 def ==(other) eql? other end |
#as_json ⇒ Object
78 79 80 |
# File 'lib/iso8601_basic/duration.rb', line 78 def as_json iso8601 end |
#blank? ⇒ Boolean
34 35 36 |
# File 'lib/iso8601_basic/duration.rb', line 34 def blank? empty? end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/iso8601_basic/duration.rb', line 38 def empty? to_h.all? { |key, value| value.zero? } end |
#eql?(other) ⇒ Boolean
58 59 60 |
# File 'lib/iso8601_basic/duration.rb', line 58 def eql?(other) hash == other.hash end |
#format ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/iso8601_basic/duration.rb', line 86 def format singular = { years: 'year', months: 'month', weeks: 'week', days: 'day', hours: 'hour', minutes: 'minute', seconds: 'second' } to_h.collect do |key, value| value = value.divmod(1)[1].zero?? value.to_i : value value.zero?? nil : key == 1.0 ? "#{value} #{singular[key]}" : "#{value} #{key}" end.compact.join(', ') end |
#hash ⇒ Object
66 67 68 |
# File 'lib/iso8601_basic/duration.rb', line 66 def hash [self.class, to_a].hash end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/iso8601_basic/duration.rb', line 70 def inspect "#<#{self.class.name}: #{iso8601}>" end |
#iso8601 ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/iso8601_basic/duration.rb', line 42 def iso8601 return nil if blank? day_parts = [:years, :months, :weeks, :days].collect do |key| value = to_h[key].divmod(1)[1].zero?? to_h[key].to_i : to_h[key] "#{value}#{key.to_s[0].upcase}" unless value.zero? end.compact time_parts = [:hours, :minutes, :seconds].collect do |key| value = to_h[key].divmod(1)[1].zero?? to_h[key].to_i : to_h[key] "#{value}#{key.to_s[0].upcase}" unless value.zero? end.compact "P#{day_parts.join}#{time_parts.any?? "T#{time_parts.join}" : ''}" end |
#present? ⇒ Boolean
30 31 32 |
# File 'lib/iso8601_basic/duration.rb', line 30 def present? ! blank? end |
#to_a ⇒ Object
118 119 120 |
# File 'lib/iso8601_basic/duration.rb', line 118 def to_a to_h.to_a end |
#to_f ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/iso8601_basic/duration.rb', line 102 def to_f { years: 31536000, months: 2628000, weeks: 604800, days: 86400, hours: 3600, minutes: 60, secodns: 1 }.collect do |key, value| to_h[key] * value end.inject :+ end |
#to_h ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/iso8601_basic/duration.rb', line 122 def to_h { years: @years, months: @months, weeks: @weeks, days: @days, hours: @hours, minutes: @minutes, seconds: @seconds } end |
#to_i ⇒ Object
114 115 116 |
# File 'lib/iso8601_basic/duration.rb', line 114 def to_i to_f.to_i end |
#to_json ⇒ Object
82 83 84 |
# File 'lib/iso8601_basic/duration.rb', line 82 def to_json "\"#{iso8601}\"" end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/iso8601_basic/duration.rb', line 74 def to_s iso8601 end |