to_duration 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c88aeaa5f019f1bf7a63698277624b3fb0406e2752344a19f5e9886ee2168f6
4
- data.tar.gz: 6821895e880e484887c691806e383b3a1b1956bffd16d71a4a9ff1445e9ebec0
3
+ metadata.gz: 3f666e8ee38f3e2774197a4efcb96cd72a3eb7a00b86acca35d931e919020a81
4
+ data.tar.gz: 68f533ca4678c8ee6352f034166bd2da22a42f7cfa950d59479326844b0cd725
5
5
  SHA512:
6
- metadata.gz: be3f3acb88afa570c9cf86e03f04c39e8afcc9d3b74ca499d7e904f4eb8ad9e58340718879c722691279a628ad35ff22cfdc87988ef6c495ebbff32d3e843e96
7
- data.tar.gz: d31929e163e45b969a6097c5f0f89cebdd1dfc899564ca01306fa074fac454353568414ee78a8368e6410c41529ef3133b935421e1696a43ad33ad81004e63f1
6
+ metadata.gz: 8f7030a9d7454278dcbc104d307945788f707c1e244bd7c2d7b1869d9a161109af45936dbe12c245b1f031469b8ffe096955ad4cdd096e9f35e150db7a0d4b4d
7
+ data.tar.gz: fd4c42b2819ee1cdfdd00db2a92324f0406ac385876e713ffafc9b5d2ec780a90310b38d7fa55167a784183f8644188b3c38ba117e303e7813d24caf7ca21ad3
@@ -1,3 +1,9 @@
1
1
  require 'to_duration/version'
2
2
  require 'to_duration/duration'
3
3
  require_relative 'numeric'
4
+
5
+ module ToDuration
6
+ def self.t(key, options = {})
7
+ I18n.t("to_duration.#{key}", options)
8
+ end
9
+ end
@@ -49,27 +49,24 @@ module ToDuration
49
49
  end
50
50
 
51
51
  def to_s(options = {})
52
- return t('less_than_one_second') if seconds < 1
52
+ return ToDuration.t('less_than_one_second') if seconds < 1
53
53
 
54
- options = {}.tap do |defaults|
55
- UNITS.keys.each { |k| defaults[k] = k != :weeks }
56
- end.merge!(options)
57
-
58
- units_to_s(to_units(options))
59
- end
60
-
61
- private
54
+ units = to_units(options).map do |k, v|
55
+ "#{v} #{ToDuration.t(k.to_s.chop, count: v)}"
56
+ end
62
57
 
63
- def t(key, options = {})
64
- I18n.t("to_duration.#{key}", options)
58
+ units.join(', ').tap do |s|
59
+ s.gsub!(/,([^,]*)$/, " #{ToDuration.t('and')}\\1") if units.length > 1
60
+ end
65
61
  end
66
62
 
67
- def to_units(options)
63
+ def to_units(options = {})
64
+ options = { weeks: false }.merge!(options)
68
65
  duration = Duration.new(seconds)
69
66
 
70
67
  {}.tap do |units|
71
68
  UNITS.keys.each do |k|
72
- next if options[k] != true
69
+ next if options.key?(k) && options[k] != true
73
70
 
74
71
  count = duration.public_send(k)
75
72
  next if count.zero?
@@ -79,13 +76,5 @@ module ToDuration
79
76
  end
80
77
  end
81
78
  end
82
-
83
- def units_to_s(units)
84
- units = units.map { |k, v| "#{v} #{t(k.to_s.chop, count: v)}" }
85
-
86
- s = units.join(', ')
87
- s.gsub!(/,([^,]*)$/, " #{t('and')}\\1") if units.length > 1
88
- s
89
- end
90
79
  end
91
80
  end
@@ -1,3 +1,3 @@
1
1
  module ToDuration
2
- VERSION = '1.2.0'.freeze
2
+ VERSION = '1.2.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikolay Digaev