duration_in_words 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 61890a8cefb328cd0317b59e381a4482cec0426059036bb2b24f40a73dccb11e
4
+ data.tar.gz: ecf684b6fca14b39e7a68126f19f85e9341f1a33ffa0ec886878223b145ae864
5
+ SHA512:
6
+ metadata.gz: da2221acad3383d7be4adf48dcbe7bf6d5fda42d120c1ce649c40cf298fc21fcc4b6275d0599706689d831151c9ef53b5aba6c0484ca2553a206422ef1dd3898
7
+ data.tar.gz: c609b68e4ba85d0c68327b346a7a2d781ac6b3beae6d4031412e85d414074cf7e0f8435718a4dcf64e089e35da7ed20bc5626143638092ead316e41417b23e12
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,136 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.6
5
+
6
+ # Commonly used screens these days easily fit more than 80 characters.
7
+ Layout/LineLength:
8
+ Max: 120
9
+
10
+ # Too short methods lead to extraction of single-use methods, which can make
11
+ # the code easier to read (by naming things), but can also clutter the class
12
+ Metrics/MethodLength:
13
+ Max: 20
14
+
15
+ # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
16
+ Metrics/ClassLength:
17
+ Max: 1500
18
+
19
+ # No space makes the method definition shorter and differentiates
20
+ # from a regular assignment.
21
+ Layout/SpaceAroundEqualsInParameterDefault:
22
+ EnforcedStyle: space
23
+
24
+ # Single quotes being faster is hardly measurable and only affects parse time.
25
+ # Enforcing double quotes reduces the times where you need to change them
26
+ # when introducing an interpolation. Use single quotes only if their semantics
27
+ # are needed.
28
+ Layout/StringLiterals:
29
+ EnforcedStyle: double_quotes
30
+
31
+ Style/StringLiteralsInInterpolation:
32
+ Enabled: true
33
+ EnforcedStyle: single_quotes
34
+
35
+ # We do not need to support Ruby 1.9, so this is good to use.
36
+ Style/SymbolArray:
37
+ Enabled: true
38
+
39
+ # Most readable form.
40
+ Style/OptionHash:
41
+ EnforcedHashRocketStyle: table
42
+ EnforcedColonStyle: table
43
+
44
+ # Mixing the styles looks just silly.
45
+ Style/HashSyntax:
46
+ EnforcedStyle: ruby19_no_mixed_keys
47
+
48
+ # has_key? and has_value? are far more readable than key? and value?
49
+ Style/PreferredHashMethods:
50
+ Enabled: false
51
+
52
+ # String#% is by far the least verbose and only object oriented variant.
53
+ Style/FormatString:
54
+ EnforcedStyle: percent
55
+
56
+ Style/CollectionMethods:
57
+ Enabled: true
58
+ PreferredMethods:
59
+ # inject seems more common in the community.
60
+ reduce: 'inject'
61
+
62
+ # Either allow this style or don't. Marking it as safe with parenthesis
63
+ # is silly. Let's try to live without them for now.
64
+ Style/ParenthesesAroundCondition:
65
+ AllowSafeAssignment: false
66
+ Lint/AssignmentInCondition:
67
+ AllowSafeAssignment: false
68
+
69
+ # A specialized exception class will take one or more arguments and construct the message from it.
70
+ # So both variants make sense.
71
+ Style/RaiseArgs:
72
+ Enabled: false
73
+
74
+ # Indenting the chained dots beneath each other is not supported by this cop,
75
+ # see https://github.com/bbatsov/rubocop/issues/1633
76
+ Style/MultilineOperationIndentation:
77
+ Enabled: false
78
+
79
+ # Fail is an alias of raise. Avoid aliases, it's more cognitive load for no gain.
80
+ # The argument that fail should be used to abort the program is wrong too,
81
+ # there's Kernel#abort for that.
82
+ Style/SignalException:
83
+ EnforcedStyle: only_raise
84
+
85
+ # Suppressing exceptions can be perfectly fine, and be it to avoid to
86
+ # explicitly type nil into the rescue since that's what you want to return,
87
+ # or suppressing LoadError for optional dependencies
88
+ Lint/SuppressedException:
89
+ Enabled: false
90
+
91
+ Layout/SpaceInsideHashLiteralBraces:
92
+ EnforcedStyle: space
93
+
94
+ # { ... } for multi-line blocks is okay, follow Weirichs rule instead:
95
+ # https://web.archive.org/web/20140221124509/http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
96
+ Style/BlockDelimiters:
97
+ Enabled: false
98
+
99
+ # do / end blocks should be used for side effects,
100
+ # methods that run a block for side effects and have
101
+ # a useful return value are rare, assign the return
102
+ # value to a local variable for those cases.
103
+ Style/MethodCalledOnDoEndBlock:
104
+ Enabled: true
105
+
106
+ # Enforcing the names of variables? To single letter ones? Just no.
107
+ Style/SingleLineBlockParams:
108
+ Enabled: false
109
+
110
+ # Shadowing outer local variables with block parameters is often useful
111
+ # to not reinvent a new name for the same thing, it highlights the relation
112
+ # between the outer variable and the parameter. The cases where it's actually
113
+ # confusing are rare, and usually bad for other reasons already, for example
114
+ # because the method is too long.
115
+ Lint/ShadowingOuterLocalVariable:
116
+ Enabled: false
117
+
118
+ # Check with yard instead.
119
+ Style/Documentation:
120
+ Enabled: false
121
+
122
+ # This is just silly. Calling the argument `other` in all cases makes no sense.
123
+ Naming/BinaryOperatorParameterName:
124
+ Enabled: false
125
+
126
+ # There are valid cases, for example debugging Cucumber steps,
127
+ # also they'll fail CI anyway
128
+ Lint/Debugger:
129
+ Enabled: false
130
+
131
+ # Style preference
132
+ Style/MethodDefParentheses:
133
+ Enabled: false
134
+
135
+ RSpec/MultipleExpectations:
136
+ Max: 2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-04
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in duration_in_words.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Syed Aslam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # DurationInWords
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/duration_in_words`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add duration_in_words
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install duration_in_words
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/duration_in_words.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module DurationHelper
6
+ include DurationInWords::Methods
7
+
8
+ # Reports the Duration object as seconds.
9
+ #
10
+ # === Options
11
+ # * <tt>:format</tt> - The format to be used in reporting the duration, :full or :compact (default: :compact)
12
+ # * <tt>:locale</tt> - If +I18n+ is available, you can set a locale and use the connector options defined on
13
+ # the 'support.array' namespace in the corresponding dictionary file.
14
+ #
15
+ # === Examples
16
+ # d = 1.day + 2.hours + 30.minutes
17
+ # duration_in_words(d) => 1d, 2h, and 30m
18
+ # d = 2.hours
19
+ # duration_in_words(d) => 2h
20
+ #
21
+ # Using <tt>:format</tt> option:
22
+ # d = 1.day + 2.hours + 30.minutes
23
+ # duration_in_words(d, format: :full) => 1 day, 2 hours, and 30 minutes
24
+ #
25
+ # Using <tt>:locale</tt> option:
26
+ # # Given this locale dictionary:
27
+ # #
28
+ # # de:
29
+ # # duration:
30
+ # # in_words:
31
+ # # format:
32
+ # # compact:
33
+ # # support:
34
+ # # words_connector: ', '
35
+ # # two_words_connector: ' und '
36
+ # # last_word_connector: ', und '
37
+ # # years:
38
+ # # one: J
39
+ # # other: ...
40
+ # # months:
41
+ # # ...
42
+ # # full:
43
+ # # ...
44
+ #
45
+ # d = 1.day + 2.hours + 30.minutes
46
+ # duration_in_words(d, locale: :de) => 1 Tag, 2 Std., und 30 Sekunden
47
+ #
48
+ def duration_in_words(duration, options = {})
49
+ DurationInWords::Methods.duration_in_words(duration, options)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,50 @@
1
+ de:
2
+ duration:
3
+ in_words:
4
+ format:
5
+ compact:
6
+ years:
7
+ one: '%{count}J'
8
+ other: '%{count}J'
9
+ months:
10
+ one: '%{count}M'
11
+ other: '%{count}M'
12
+ days:
13
+ one: '%{count}T'
14
+ other: '%{count}T'
15
+ hours:
16
+ one: '%{count}Std.'
17
+ other: '%{count}Std.'
18
+ minutes:
19
+ one: '%{count}Min'
20
+ other: '%{count}Min'
21
+ seconds:
22
+ one: '%{count}s'
23
+ other: '%{count}s'
24
+ support:
25
+ words_connector: ' '
26
+ two_words_connector: ' und '
27
+ last_word_connector: ' und '
28
+ full:
29
+ years:
30
+ one: '%{count} Jahr'
31
+ other: '%{count} Jahre'
32
+ months:
33
+ one: '%{count} Monat'
34
+ other: '%{count} Monate'
35
+ days:
36
+ one: '%{count} Tag'
37
+ other: '%{count} Tage'
38
+ hours:
39
+ one: '%{count} Stunde'
40
+ other: '%{count} Stunden'
41
+ minutes:
42
+ one: '%{count} Minute'
43
+ other: '%{count} Minuten'
44
+ seconds:
45
+ one: '%{count} Sekunde'
46
+ other: '%{count} Sekunden'
47
+ support:
48
+ words_connector: ' '
49
+ two_words_connector: ' und '
50
+ last_word_connector: ' und '
@@ -0,0 +1,50 @@
1
+ en:
2
+ duration:
3
+ in_words:
4
+ format:
5
+ compact:
6
+ years:
7
+ one: '%{count}yr.'
8
+ other: '%{count}yrs.'
9
+ months:
10
+ one: '%{count}mo.'
11
+ other: '%{count}mos.'
12
+ days:
13
+ one: '%{count}d'
14
+ other: '%{count}d'
15
+ hours:
16
+ one: '%{count}h'
17
+ other: '%{count}h'
18
+ minutes:
19
+ one: '%{count}m'
20
+ other: '%{count}m'
21
+ seconds:
22
+ one: '%{count}s'
23
+ other: '%{count}s'
24
+ support:
25
+ words_connector: ' '
26
+ two_words_connector: ' and '
27
+ last_word_connector: ' and '
28
+ full:
29
+ years:
30
+ one: '%{count} year'
31
+ other: '%{count} years'
32
+ months:
33
+ one: '%{count} month'
34
+ other: '%{count} months'
35
+ days:
36
+ one: '%{count} day'
37
+ other: '%{count} days'
38
+ hours:
39
+ one: '%{count} hour'
40
+ other: '%{count} hours'
41
+ minutes:
42
+ one: '%{count} minute'
43
+ other: '%{count} minutes'
44
+ seconds:
45
+ one: '%{count} second'
46
+ other: '%{count} seconds'
47
+ support:
48
+ words_connector: ', '
49
+ two_words_connector: ' and '
50
+ last_word_connector: ', and '
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DurationInWords
4
+ module Methods
5
+ extend self
6
+
7
+ def duration_in_words(duration, options = {})
8
+ raise_type_error(duration) unless duration.is_a?(ActiveSupport::Duration)
9
+
10
+ locale, scope = parse_options(options)
11
+ parts = duration.parts
12
+
13
+ if parts.empty?
14
+ key = "seconds.#{duration.value == 1 ? 'one' : 'other'}"
15
+ return I18n.t(key, count: duration.value, scope: scope, locale: locale)
16
+ end
17
+
18
+ sentencify(parts, scope, locale)
19
+ end
20
+
21
+ private
22
+
23
+ def parse_options(options)
24
+ format = options.fetch(:format, :compact)
25
+ locale = options.fetch(:locale, :en)
26
+
27
+ scope = format.to_s == "full" ? I18N_SCOPE_FULL : DEFAULT_I18N_SCOPE
28
+
29
+ [locale, scope]
30
+ end
31
+
32
+ def sentencify(parts, scope, locale)
33
+ parts
34
+ .sort_by { |unit, _| ActiveSupport::Duration::PARTS.index(unit) }
35
+ .map { |unit, val|
36
+ case val
37
+ when 1 then I18n.t("#{unit}.one", count: val, scope: scope, locale: locale)
38
+ else I18n.t("#{unit}.other", count: val, scope: scope, locale: locale)
39
+ end
40
+ }
41
+ .to_sentence(sentence_options(scope, locale))
42
+ end
43
+
44
+ def sentence_options(scope, locale)
45
+ I18n.t(:support, scope: scope, locale: locale, default: { locale: locale })
46
+ end
47
+
48
+ def raise_type_error(type)
49
+ raise TypeError, "no implicit conversion of #{type.class} into ActiveSupport::Duration"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DurationInWords
4
+ VERSION = "0.2.0"
5
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "i18n"
4
+ require "active_support"
5
+ require "active_support/core_ext"
6
+
7
+ require_relative "duration_in_words/version"
8
+
9
+ module DurationInWords
10
+ extend ActiveSupport::Autoload
11
+
12
+ eager_autoload do
13
+ autoload :Methods, "duration_in_words/methods"
14
+ end
15
+
16
+ I18N_SCOPE_FULL = :'duration.in_words.format.full'
17
+ DEFAULT_I18N_SCOPE = :'duration.in_words.format.compact'
18
+
19
+ module_function
20
+
21
+ def setup_i18n!
22
+ locale_files = Dir[File.join File.dirname(__FILE__), "duration_in_words/locales", "*.yml"]
23
+
24
+ I18n.load_path.unshift(*locale_files)
25
+ I18n.reload!
26
+ end
27
+ end
28
+
29
+ DurationInWords.setup_i18n!
30
+
31
+ begin
32
+ require "action_view"
33
+ require_relative "duration_in_words/action_view/helpers/duration_helper"
34
+ rescue LoadError
35
+ end
@@ -0,0 +1,4 @@
1
+ module DurationInWords
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duration_in_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Syed Aslam
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.12.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.12.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.18.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.18.1
97
+ description: |-
98
+ Convert ActiveSupport::Duration objects to concise human readable formats like '2h 30m 45s'
99
+ with locale support.
100
+ email:
101
+ - aslam.maqsood@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - CHANGELOG.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/duration_in_words.rb
114
+ - lib/duration_in_words/action_view/helpers/duration_helper.rb
115
+ - lib/duration_in_words/locales/de.yml
116
+ - lib/duration_in_words/locales/en.yml
117
+ - lib/duration_in_words/methods.rb
118
+ - lib/duration_in_words/version.rb
119
+ - sig/duration_in_words.rbs
120
+ homepage: https://github.com/aslam/duration_in_words
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ homepage_uri: https://github.com/aslam/duration_in_words
125
+ source_code_uri: https://github.com/aslam/duration_in_words
126
+ changelog_uri: https://github.com/aslam/duration_in_words/blob/main/CHANGELOG.md
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: 2.6.0
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.3.7
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Report ActiveSupport::Duration in concise human readable formats.
146
+ test_files: []