class ISO8601Basic::Duration
Attributes
days[R]
hours[R]
minutes[R]
months[R]
seconds[R]
sign[R]
weeks[R]
years[R]
Public Class Methods
new(value)
click to toggle source
# 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
Public Instance Methods
==(other)
click to toggle source
# File lib/iso8601_basic/duration.rb, line 62 def ==(other) eql? other end
as_json()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 78 def as_json iso8601 end
blank?()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 34 def blank? empty? end
empty?()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 38 def empty? to_h.all? { |key, value| value.zero? } end
eql?(other)
click to toggle source
# File lib/iso8601_basic/duration.rb, line 58 def eql?(other) hash == other.hash end
format()
click to toggle source
# 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()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 66 def hash [self.class, to_a].hash end
inspect()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 70 def inspect "#<#{self.class.name}: #{iso8601}>" end
iso8601()
click to toggle source
# 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?()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 30 def present? ! blank? end
to_a()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 118 def to_a to_h.to_a end
to_f()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 114 def to_i to_f.to_i end
to_json()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 82 def to_json "\"#{iso8601}\"" end
to_s()
click to toggle source
# File lib/iso8601_basic/duration.rb, line 74 def to_s iso8601 end