From 691ba10cd0bf316cba87842fae3ad334530d5a7d Mon Sep 17 00:00:00 2001 From: Madh93 Date: Mon, 23 Apr 2018 08:01:46 +0100 Subject: [PATCH] Add short format option --- lib/read_my_time/reader.rb | 2 ++ test/test_read_my_time.rb | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/read_my_time/reader.rb b/lib/read_my_time/reader.rb index 25ce9d6..4dd6661 100644 --- a/lib/read_my_time/reader.rb +++ b/lib/read_my_time/reader.rb @@ -8,6 +8,7 @@ def seconds_in_words(opts = {}) # options by default opts[:skip_seconds] = false if opts[:skip_seconds].nil? + opts[:short_format] = false if opts[:short_format].nil? unit_time_dividers.each_with_object([]) do |(unit_time, divider), s| @@ -16,6 +17,7 @@ def seconds_in_words(opts = {}) if rest.nonzero? && !(unit_time == :seconds && opts[:skip_seconds]) word = I18n.t(unit_time.to_s, count: rest, scope: locale_prefix) + word = (opts[:short_format] ? word[0] : word) s.unshift("#{rest} #{word}") end end diff --git a/test/test_read_my_time.rb b/test/test_read_my_time.rb index c828a74..c8aa155 100644 --- a/test/test_read_my_time.rb +++ b/test/test_read_my_time.rb @@ -20,6 +20,10 @@ def test_dont_return_seconds_explicitly assert_equal('', 10.seconds_in_words(skip_seconds: true)) end + def test_return_seconds_using_short_format + assert_equal('10 s', 10.seconds_in_words(short_format: true)) + end + def test_handle_float_values assert_equal('10 seconds', 10.1.seconds_in_words) end @@ -40,6 +44,10 @@ def test_return_word_days assert_equal('1 day 1 hour 1 minute 40 seconds', (1.day + 1.hour + 1.minute + 40).seconds_in_words) end + def test_return_word_days_using_short_format + assert_equal('1 d 1 h 1 m 40 s', (1.day + 1.hour + 1.minute + 40).seconds_in_words(short_format: true)) + end + # skip show 0 values def test_dont_return_zero_values assert_equal('1 day 1 minute 40 seconds', (1.day + 1.minute + 40).seconds_in_words)