From c5fbfc81847fb8ce47064792cb7400532e70894f Mon Sep 17 00:00:00 2001 From: Alfred Dominic Date: Thu, 13 Aug 2020 14:35:21 +0530 Subject: [PATCH] add option to set precision --- lib/time_difference.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/time_difference.rb b/lib/time_difference.rb index 589e202..b9afe95 100644 --- a/lib/time_difference.rb +++ b/lib/time_difference.rb @@ -45,9 +45,9 @@ def in_each_component end] end - def in_general + def in_general(precision: :seconds) remaining = @time_diff - Hash[TIME_COMPONENTS.map do |time_component| + Hash[TIME_COMPONENTS[0..TIME_COMPONENTS.index(precision)].map do |time_component| if remaining > 0 rounded_time_component = (remaining / 1.send(time_component).seconds).round(2).floor remaining -= rounded_time_component.send(time_component) @@ -58,9 +58,10 @@ def in_general end] end - def humanize + def humanize(precision: :seconds) diff_parts = [] - in_general.each do |part,quantity| + + in_general(precision: precision).each do |part,quantity| next if quantity <= 0 part = part.to_s.humanize