duration_in_words 0.3.1 → 0.5.0

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: 2e0b74fb2b1428b25bd58dd451ceb93073f58fa803c8f135a3d0661983dd07ab
4
- data.tar.gz: 6439cb0650d16395fa3ec21ce206e6c7e03fce4dacc9829c27e6bae3a6cc2782
3
+ metadata.gz: d7b8a55387a6b973edd5e7953a5f563e469be39e03ae3e5fe84a83fbe00b4aa2
4
+ data.tar.gz: b382175e5c913c05521f7b2b00cee53c7375078c70ef132504fcdfc37b7fc1ea
5
5
  SHA512:
6
- metadata.gz: ae86b0118a46b1a254d26d7bac7ce5376caaf30df65e76409326ccb8d771c0f511e1b113b126fe46ec424f25c1ca29519107c7d96cbd9384b0878eb7fbb1e9a0
7
- data.tar.gz: a453cdea084d270100a7c0f0cfa984f07b55c428787365e4c2a36ab733928ef1914d2eb663cd0a1ef149c7a1b759a2416ff595c38e044bcdd2dd74bd1ea798f1
6
+ metadata.gz: e0e48f6eb399ea124fc8fd5e1bbad64edea8add1d64823000e36ed9f170fc582b254d1cf5c563a7cc4958e1878c655c98f9f162b7187c4e9f6bf3ffa30fc61c9
7
+ data.tar.gz: 5cf55fb1b2c99e96d29b804cd659c533f9ad184bdf0aa1172ca0c214ad5e647420f35d897fd3b4e81a61cd1f7b4a2336200964933685ac3821361164eb10ca6c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.0] - 2026-10-30
4
+
5
+ ### Added
6
+
7
+ - `duration_in_words` accepts an ISO 8601 duration string (e.g. `"PT1H30M"`),
8
+ parsed via `ActiveSupport::Duration.parse`.
9
+ - Opt-in core extension: `require "duration_in_words/core_ext"` adds
10
+ `ActiveSupport::Duration#in_words`.
11
+
12
+ ## [0.4.0] - 2026-07-14
13
+
14
+ ### Fixed
15
+
16
+ - Stop calling `I18n.reload!` when setting up locale files. It was discarding
17
+ any translations the host app had already stored in memory (e.g. via
18
+ `I18n.backend.store_translations`), not just ones loaded from files.
19
+ - Removed trailing whitespace from the `de` locale file.
20
+
21
+ ### Changed
22
+
23
+ - `duration_in_words` now raises `ArgumentError` for an unrecognized `:format`
24
+ value instead of silently falling back to `:compact`.
25
+ - CI now runs the test suite against a matrix of supported Ruby versions
26
+ (2.6, 3.1, 3.4) instead of a single pinned version, and runs RuboCop and
27
+ `bundler-audit` as separate jobs.
28
+ - Expanded the RBS signatures beyond `VERSION` to cover
29
+ `DurationInWords::Methods` and the `ActionView` helper.
30
+
3
31
  ## [0.3.1] - 2026-03-28
4
32
 
5
33
  ### Fixed
data/README.md CHANGED
@@ -58,6 +58,24 @@ include ActionView::Helpers::DurationHelper
58
58
  => "2.5m"
59
59
  ```
60
60
 
61
+ An ISO 8601 duration string is parsed with `ActiveSupport::Duration.parse`:
62
+
63
+ ```ruby
64
+ >> duration_in_words("P1DT2H30M")
65
+ => "1d 2h and 30m"
66
+ ```
67
+
68
+ ### Core extension
69
+
70
+ Opt in to calling it directly on a duration:
71
+
72
+ ```ruby
73
+ require "duration_in_words/core_ext"
74
+
75
+ >> (1.day + 2.hours).in_words(format: :full)
76
+ => "1 day and 2 hours"
77
+ ```
78
+
61
79
  ### Using <tt>:format</tt> option:
62
80
 
63
81
  There are two formats available, `:compact`, and `:full`. `:compact` being the default.
@@ -68,6 +86,8 @@ There are two formats available, `:compact`, and `:full`. `:compact` being the d
68
86
  => "1 day, 2 hours, and 30 minutes"
69
87
  ```
70
88
 
89
+ Passing anything other than `:compact` or `:full` (as a symbol or string) raises `ArgumentError`.
90
+
71
91
  ### Using <tt>:locale</tt> option:
72
92
 
73
93
  If you do not pass `:locale`, the helper uses the current `I18n.locale`.
@@ -86,6 +106,9 @@ de:
86
106
  months:
87
107
  one: '%{count}M'
88
108
  other: '%{count}M'
109
+ weeks:
110
+ one: '%{count}W'
111
+ other: '%{count}W'
89
112
  days:
90
113
  one: '%{count}T'
91
114
  other: '%{count}T'
@@ -109,6 +132,9 @@ de:
109
132
  months:
110
133
  one: '%{count} Monat'
111
134
  other: '%{count} Monate'
135
+ weeks:
136
+ one: '%{count} Woche'
137
+ other: '%{count} Wochen'
112
138
  days:
113
139
  one: '%{count} Tag'
114
140
  other: '%{count} Tage'
data/Rakefile CHANGED
@@ -9,4 +9,8 @@ require "rubocop/rake_task"
9
9
 
10
10
  RuboCop::RakeTask.new
11
11
 
12
+ require "bundler/audit/task"
13
+
14
+ Bundler::Audit::Task.new
15
+
12
16
  task default: %i[spec rubocop]
@@ -34,10 +34,13 @@ Gem::Specification.new do |spec|
34
34
  spec.add_dependency "activesupport", ">= 6.0"
35
35
  spec.add_dependency "i18n", "~> 1.12"
36
36
 
37
+ spec.add_development_dependency "bundler-audit"
37
38
  spec.add_development_dependency "racc"
38
39
  spec.add_development_dependency "rake", "~> 13.0"
39
40
  spec.add_development_dependency "rspec", "~> 3.0"
40
41
  spec.add_development_dependency "rubocop", "~> 1.21"
41
42
  spec.add_development_dependency "rubocop-rake"
42
43
  spec.add_development_dependency "rubocop-rspec", "~> 2.18.1"
44
+ # ponytail: rubocop-capybara 2.23 breaks rubocop-rspec 2.18; drop when moving to rubocop-rspec 3.x
45
+ spec.add_development_dependency "rubocop-capybara", "< 2.23"
43
46
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "duration_in_words"
4
+
5
+ module ActiveSupport
6
+ class Duration
7
+ def in_words(options = {})
8
+ DurationInWords::Methods.duration_in_words(self, options)
9
+ end
10
+ end
11
+ end
@@ -37,7 +37,7 @@ de:
37
37
  other: '%{count} Monate'
38
38
  weeks:
39
39
  one: '%{count} Woche'
40
- other: '%{count} Wochen'
40
+ other: '%{count} Wochen'
41
41
  days:
42
42
  one: '%{count} Tag'
43
43
  other: '%{count} Tage'
@@ -4,7 +4,10 @@ module DurationInWords
4
4
  module Methods
5
5
  extend self
6
6
 
7
+ VALID_FORMATS = %i[compact full].freeze
8
+
7
9
  def duration_in_words(duration, options = {})
10
+ duration = ActiveSupport::Duration.parse(duration) if duration.is_a?(String)
8
11
  raise_type_error(duration) unless duration.is_a?(ActiveSupport::Duration)
9
12
 
10
13
  locale, scope = parse_options(options)
@@ -18,14 +21,23 @@ module DurationInWords
18
21
  private
19
22
 
20
23
  def parse_options(options)
21
- format = options.fetch(:format, :compact)
24
+ format = normalize_format(options.fetch(:format, :compact))
22
25
  locale = options.fetch(:locale, I18n.locale)
23
26
 
24
- scope = format.to_s == "full" ? I18N_SCOPE_FULL : DEFAULT_I18N_SCOPE
27
+ scope = format == :full ? I18N_SCOPE_FULL : DEFAULT_I18N_SCOPE
25
28
 
26
29
  [locale, scope]
27
30
  end
28
31
 
32
+ def normalize_format(format)
33
+ symbol = format.respond_to?(:to_sym) ? format.to_sym : format
34
+
35
+ return symbol if VALID_FORMATS.include?(symbol)
36
+
37
+ raise ArgumentError,
38
+ "invalid :format, #{format.inspect} given. Valid formats: #{VALID_FORMATS.map(&:inspect).join(', ')}"
39
+ end
40
+
29
41
  def sentencify(parts, scope, locale)
30
42
  parts
31
43
  .sort_by { |unit, _| ActiveSupport::Duration::PARTS.index(unit) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DurationInWords
4
- VERSION = "0.3.1"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "logger" # activesupport < 7.1 needs it loaded first; concurrent-ruby >= 1.3.5 stopped requiring it
3
4
  require "i18n"
4
5
  require "active_support"
5
6
  require "active_support/core_ext"
@@ -22,7 +23,6 @@ module DurationInWords
22
23
  locale_files = Dir[File.join File.dirname(__FILE__), "duration_in_words/locales", "*.yml"]
23
24
 
24
25
  I18n.load_path.unshift(*locale_files)
25
- I18n.reload!
26
26
  end
27
27
  end
28
28
 
@@ -1,4 +1,42 @@
1
1
  module DurationInWords
2
2
  VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
3
+
4
+ I18N_SCOPE_FULL: Symbol
5
+ DEFAULT_I18N_SCOPE: Symbol
6
+
7
+ def self.setup_i18n!: () -> void
8
+
9
+ module Methods
10
+ VALID_FORMATS: Array[Symbol]
11
+
12
+ def duration_in_words: (untyped duration, ?Hash[Symbol, untyped] options) -> String
13
+
14
+ private
15
+
16
+ def parse_options: (Hash[Symbol, untyped] options) -> [untyped, Symbol]
17
+
18
+ def normalize_format: (untyped format) -> Symbol
19
+
20
+ def sentencify: (Hash[Symbol, untyped] parts, Symbol scope, untyped locale) -> String
21
+
22
+ def sentence_options: (Symbol scope, untyped locale) -> untyped
23
+
24
+ def raise_type_error: (untyped type) -> bot
25
+ end
26
+ end
27
+
28
+ module ActiveSupport
29
+ class Duration
30
+ def in_words: (?Hash[Symbol, untyped] options) -> String
31
+ end
32
+ end
33
+
34
+ module ActionView
35
+ module Helpers
36
+ module DurationHelper
37
+ include DurationInWords::Methods
38
+
39
+ def duration_in_words: (untyped duration, ?Hash[Symbol, untyped] options) -> String
40
+ end
41
+ end
4
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duration_in_words
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Aslam
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '1.12'
40
+ - !ruby/object:Gem::Dependency
41
+ name: bundler-audit
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: racc
42
56
  requirement: !ruby/object:Gem::Requirement
@@ -121,6 +135,20 @@ dependencies:
121
135
  - - "~>"
122
136
  - !ruby/object:Gem::Version
123
137
  version: 2.18.1
138
+ - !ruby/object:Gem::Dependency
139
+ name: rubocop-capybara
140
+ requirement: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "<"
143
+ - !ruby/object:Gem::Version
144
+ version: '2.23'
145
+ type: :development
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "<"
150
+ - !ruby/object:Gem::Version
151
+ version: '2.23'
124
152
  description: |-
125
153
  Convert ActiveSupport::Duration objects to concise human readable formats like '2h 30m 45s'
126
154
  with locale support.
@@ -140,6 +168,7 @@ files:
140
168
  - duration_in_words.gemspec
141
169
  - lib/duration_in_words.rb
142
170
  - lib/duration_in_words/action_view/helpers/duration_helper.rb
171
+ - lib/duration_in_words/core_ext.rb
143
172
  - lib/duration_in_words/locales/de.yml
144
173
  - lib/duration_in_words/locales/en.yml
145
174
  - lib/duration_in_words/methods.rb