From 09bbef910dd3de1239b077798fe63560d448c4aa Mon Sep 17 00:00:00 2001 From: Alfred Dominic Date: Thu, 13 Aug 2020 02:42:41 +0530 Subject: [PATCH] add option to set precision level for humanized output --- lib/time_difference.rb | 9 +++++---- time_difference.gemspec | 2 +- 2 files changed, 6 insertions(+), 5 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 diff --git a/time_difference.gemspec b/time_difference.gemspec index 5ece9c4..f0f6551 100644 --- a/time_difference.gemspec +++ b/time_difference.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |gem| gem.version = "0.7.0" gem.license = 'MIT' - gem.add_runtime_dependency('activesupport', '~> 5.1') + gem.add_runtime_dependency('activesupport') gem.add_development_dependency('rspec', '~> 3.7.0') gem.add_development_dependency('rake')