class TimeDistance::Calculator
Constants
- DEFAULTS
- MULTIPLES
Attributes
options[R]
Public Class Methods
new(time_from, time_to, options = {})
click to toggle source
# File lib/time_distance/calculator.rb, line 24 def initialize(time_from, time_to, options = {}) @time_from = time_from @time_to = time_to @options = DEFAULTS.merge(options) @seconds = @time_from - @time_to end
Public Instance Methods
to_s()
click to toggle source
# File lib/time_distance/calculator.rb, line 32 def to_s array = to_a.compact array << I18n.t(:ago, scope: options[:translation_scope]) if negative? array.join(' ') end
units()
click to toggle source
# File lib/time_distance/calculator.rb, line 39 def units @units ||= calculate end
Private Instance Methods
calculate()
click to toggle source
# File lib/time_distance/calculator.rb, line 62 def calculate values = {} desc_sorted_unit_format.inject(@seconds.abs.to_f.round) do |remainder, unit| multiple = MULTIPLES[unit] values[unit] = remainder / multiple remainder % multiple end values end
desc_sorted_unit_format()
click to toggle source
# File lib/time_distance/calculator.rb, line 58 def desc_sorted_unit_format options[:format].sort { |p, n| MULTIPLES[n] <=> MULTIPLES[p] } end
negative?()
click to toggle source
# File lib/time_distance/calculator.rb, line 54 def negative? @seconds.to_i < 0 end
to_a()
click to toggle source
# File lib/time_distance/calculator.rb, line 45 def to_a options[:format].map do |unit| value = units[unit].to_i next unless value > 0 I18n.t(unit, scope: [options[:translation_scope], :units], count: value) end end