dotiw 5.3.2 → 5.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bbd17f6b2f3af46a5255fa9eb900aa7bb734ead661088a2936739aa2f2bef63
4
- data.tar.gz: 2870eadd1e540d51e6ff73210024fbcb87de8c01cca2549d7fb80e30b4390d93
3
+ metadata.gz: 156f786ed11b28af2e890b6612fb07fb09bd462dc818a5bb8bb9075a518044d7
4
+ data.tar.gz: ca01c5a48869011c7c1fe9f08cf8c6ad953920b4ea5bc4dd26e21850131b6363
5
5
  SHA512:
6
- metadata.gz: a89bfcad7275d64474969ac9200f43d6b5b3fb922c6cfa9e4bc05550361f43dc3d1eb37c27771116dc51cfba6adfa844232fbc062592dfa2874ff0e355797c3b
7
- data.tar.gz: 8379db832851f15302fa364103671c3d805c0f367d8c8c0be4d746e81a473ab4b7d49c29a248fd353410a14e22ed04db79a1bfb05b83f4fd52403781460712fa
6
+ metadata.gz: 807d4628412b8d810c072ea5cf4e10ed2cb7e280bdeaeb1b65906f1e203bd1155c5a7457d21d4c8c8c112a5a7d0a9b9b9a3c9ff5f1100d6ff0a255f64380a1f3
7
+ data.tar.gz: 78c14c9c5ea09eb378a380fe686672e925665fdebd9f75096a5cdecc1f9d8497d9cf7839a8cad4dd9d84e4938441e1380c076ecb3ccbf0a24359aa70993da9a2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 5.3.3 (2022/04/25)
2
+
3
+ * [#128](https://github.com/radar/distance_of_time_in_words/pull/128): Adds support for numeric values for both the `from_time` and `to_time` arguments in the `#distance_of_time_in_words` helper - [@bjedrocha](https://github.com/bjedrocha).
4
+
1
5
  ## 5.3.2 (2021/11/08)
2
6
 
3
7
  * [#126](https://github.com/radar/distance_of_time_in_words/pull/126): Fixes `#distance_of_time_in_words_to_now` with `vague: true` when supplied without `include_seconds` argument - [@mozcomp](https://github.com/mozcomp).
data/README.markdown CHANGED
@@ -58,6 +58,22 @@ Better than "about 1 year", am I right? Of course I am.
58
58
  => "1 second"
59
59
  ```
60
60
 
61
+ It also supports numeric arguments like the original Rails version:
62
+
63
+ ```ruby
64
+ >> distance_of_time_in_words(0, 150)
65
+ => "2 minutes and 30 seconds"
66
+ ```
67
+
68
+ as an alternative to:
69
+
70
+ ```ruby
71
+ >> distance_of_time_in_words(Time.now, Time.now + 2.5.minutes)
72
+ => "2 minutes and 30 seconds"
73
+ ```
74
+
75
+ This is useful if you're just interested in "stringifying" the length of time. Alternatively, you can use the `#distance_of_time` helper as described [below](#distance\_of\_time).
76
+
61
77
  The third argument for this method is whether or not to include seconds. By default this is `false` (because in Rails' `distance_of_time_in_words` it is), you can turn it on though by passing `true` as the third argument:
62
78
 
63
79
  ```ruby
data/lib/dotiw/methods.rb CHANGED
@@ -20,7 +20,9 @@ module DOTIW
20
20
  end
21
21
 
22
22
  def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {})
23
- raise ArgumentError, "nil can't be converted to a Time value" if from_time.nil? || to_time.nil?
23
+ from_time = normalize_distance_of_time_argument_to_time(from_time)
24
+ to_time = normalize_distance_of_time_argument_to_time(to_time)
25
+ from_time, to_time = to_time, from_time if from_time > to_time
24
26
 
25
27
  if include_seconds_or_options.is_a?(Hash)
26
28
  options = include_seconds_or_options
@@ -28,7 +30,8 @@ module DOTIW
28
30
  options = options.dup
29
31
  options[:include_seconds] ||= !!include_seconds_or_options
30
32
  end
31
- return distance_of_time(from_time, options) if to_time == 0
33
+
34
+ return distance_of_time(to_time.to_i, options) if from_time.to_i == 0
32
35
 
33
36
  options = options_with_scope(options)
34
37
  hash = distance_of_time_in_words_hash(from_time, to_time, options)
@@ -41,6 +44,16 @@ module DOTIW
41
44
 
42
45
  private
43
46
 
47
+ def normalize_distance_of_time_argument_to_time(value)
48
+ if value.is_a?(Numeric)
49
+ Time.at(value)
50
+ elsif value.respond_to?(:to_time)
51
+ value.to_time
52
+ else
53
+ raise ArgumentError, "#{value.inspect} can't be converted to a Time value"
54
+ end
55
+ end
56
+
44
57
  def options_with_scope(options)
45
58
  if options.key?(:compact)
46
59
  options.merge(scope: DOTIW::DEFAULT_I18N_SCOPE_COMPACT)
data/lib/dotiw/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DOTIW
4
- VERSION = '5.3.2'
4
+ VERSION = '5.3.3'
5
5
  end
@@ -101,11 +101,6 @@ describe 'A better distance_of_time_in_words' do
101
101
  expect(DOTIW.languages.map(&:to_s).sort).to eq languages.sort
102
102
  end
103
103
 
104
- it 'raises ArgumentError when nil is passed for time' do
105
- expect { distance_of_time_in_words(nil) }.to raise_error(ArgumentError)
106
- expect { distance_of_time_in_words(nil, nil) }.to raise_error(ArgumentError)
107
- end
108
-
109
104
  DOTIW.languages.each do |lang|
110
105
  context lang do
111
106
  YAML.safe_load(
@@ -241,6 +236,38 @@ describe 'A better distance_of_time_in_words' do
241
236
  end
242
237
  end
243
238
  end
239
+
240
+ describe 'with mixed inputs' do
241
+ # Further backwards compatability with the original rails
242
+ # distance_of_time_in_words helper.
243
+ [
244
+ [0, 2.5.minutes.to_i, '2 minutes and 30 seconds'],
245
+ [10.minutes.to_i, 0, '10 minutes'],
246
+ [0, 24.weeks.to_i, '5 months, 2 weeks, and 1 day'],
247
+ [0, 0, 'less than 1 second'],
248
+ ].each do |start, finish, output|
249
+ it "should be #{output}" do
250
+ expect(distance_of_time_in_words(start, finish)).to eq(output)
251
+ end
252
+ end
253
+
254
+ context "of which one or both can't be converted to a Time value" do
255
+ let(:invalid_inputs) do
256
+ [
257
+ [nil, 5.minutes],
258
+ [nil, double(:does_not_respond_to_time)],
259
+ [nil],
260
+ [nil, nil],
261
+ ]
262
+ end
263
+
264
+ it "should raise an ArgumentError" do
265
+ invalid_inputs.each do |start, finish|
266
+ expect { distance_of_time_in_words(start, finish) }.to raise_error(ArgumentError)
267
+ end
268
+ end
269
+ end
270
+ end
244
271
  end
245
272
 
246
273
  describe 'with output options' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotiw
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.3.2
4
+ version: 5.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bigg
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-11-08 00:00:00.000000000 Z
12
+ date: 2022-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport