to_duration 1.1.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dca915b8cbc3a04cccec1bd1bac0a0984ed449b9
4
- data.tar.gz: 3ee511fdca5a3c3a5488c0559216613497504f67
2
+ SHA256:
3
+ metadata.gz: 7c88aeaa5f019f1bf7a63698277624b3fb0406e2752344a19f5e9886ee2168f6
4
+ data.tar.gz: 6821895e880e484887c691806e383b3a1b1956bffd16d71a4a9ff1445e9ebec0
5
5
  SHA512:
6
- metadata.gz: 31843ff1fc0a5c024d16a20a28a50c3d1bb01db0bab2fcd00b9cb3640db2eea2d26ad4456f5c49b76f0ab6c467788d22899f90ca5dd61a401d7d9d7384aa1d48
7
- data.tar.gz: d0687a8979a7c45bbbe21af8915976facde57d24f9d41c731fc3a09242b4efcd2098fb95bb47b608877b4f58aae43d2130e55420287bc4ac875f7bb28d4a8a8a
6
+ metadata.gz: be3f3acb88afa570c9cf86e03f04c39e8afcc9d3b74ca499d7e904f4eb8ad9e58340718879c722691279a628ad35ff22cfdc87988ef6c495ebbff32d3e843e96
7
+ data.tar.gz: d31929e163e45b969a6097c5f0f89cebdd1dfc899564ca01306fa074fac454353568414ee78a8368e6410c41529ef3133b935421e1696a43ad33ad81004e63f1
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.2
5
- before_install: gem install bundler -v 1.13.6
4
+ - 2.2.10
5
+ - 2.3.7
6
+ - 2.4.4
7
+ - 2.5.1
8
+ before_install: gem install bundler -v '~> 1.16'
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/to_duration.svg)](https://badge.fury.io/rb/to_duration) [![Build Status](https://travis-ci.org/digaev/to_duration.svg)](https://travis-ci.org/digaev/to_duration) [![Coverage Status](https://coveralls.io/repos/github/digaev/to_duration/badge.svg?branch=master)](https://coveralls.io/github/digaev/to_duration?branch=master)
4
4
 
5
- A Ruby gem for converting seconds into human-readable format. This gem adds instance method `#to_duration` to `Numeric`.
5
+ A Ruby gem for converting seconds into human-readable format.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,6 +22,8 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ Just call `#to_duration` method on any `Numeric` instance.
26
+
25
27
  ```ruby
26
28
  irb(main):001:0> 12345.to_duration
27
29
  => "3 hours, 25 minutes and 45 seconds"
@@ -29,50 +31,61 @@ irb(main):001:0> 12345.to_duration
29
31
  irb(main):002:0> 1234567890.to_duration
30
32
  => "39 years, 1 month, 14 days, 5 hours, 31 minutes and 30 seconds"
31
33
 
32
- irb(main):003:0> 1234567890.to_duration(week: true)
34
+ irb(main):003:0> 1234567890.to_duration(weeks: true)
33
35
  => "39 years, 1 month, 2 weeks, 5 hours, 31 minutes and 30 seconds"
36
+
37
+ irb(main):004:0> 0.5.to_duration
38
+ => "Less than one second"
34
39
  ```
35
40
 
36
- ## Options
41
+ ### #to_duration(options = {})
37
42
 
38
- * `year` - default `true`
39
- * `month` - default `true`
40
- * `week` - default `false`
41
- * `day` - default `true`
42
- * `hour` - default `true`
43
- * `minute` - default `true`
44
- * `second` - default `true`
43
+ | Option | Type | Default |
44
+ |---|:-:|:-:|
45
+ | `:years` | Boolean | `true` |
46
+ | `:months` | Boolean | `true` |
47
+ | `:weeks` | Boolean | `false` |
48
+ | `:days` | Boolean | `true` |
49
+ | `:hours` | Boolean | `true` |
50
+ | `:minutes` | Boolean | `true` |
51
+ | `:seconds` | Boolean | `true` |
52
+
53
+ **Notice:** Before version `1.2.0` these keys were singular.
45
54
 
46
55
  ## Localization
47
56
 
48
- In your Rails application edit `config/locales/#{locale}.yml`:
57
+ The gem uses [I18n](https://github.com/svenfuchs/i18n) for localization.
49
58
 
50
59
  ```yml
51
- to_duration:
52
- and: and
53
- year:
54
- one: year
55
- other: years
56
- month:
57
- one: month
58
- other: months
59
- week:
60
- one: week
61
- other: weeks
62
- day:
63
- one: day
64
- other: days
65
- hour:
66
- one: hour
67
- other: hours
68
- minute:
69
- one: minute
70
- other: minutes
71
- second:
72
- one: second
73
- other: seconds
60
+ en: # Replace this key with your locale
61
+ to_duration:
62
+ and: and
63
+ less_than_one_second: Less than one second
64
+ year:
65
+ one: year
66
+ other: years
67
+ month:
68
+ one: month
69
+ other: months
70
+ week:
71
+ one: week
72
+ other: weeks
73
+ day:
74
+ one: day
75
+ other: days
76
+ hour:
77
+ one: hour
78
+ other: hours
79
+ minute:
80
+ one: minute
81
+ other: minutes
82
+ second:
83
+ one: second
84
+ other: seconds
74
85
  ```
75
86
 
87
+ If you're on Rails you may create `config/locales/to_duration_#{locale}.yml` file.
88
+
76
89
  ## Contributing
77
90
 
78
91
  Bug reports and pull requests are welcome on GitHub at https://github.com/digaev/to_duration.
data/lib/numeric.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Numeric
2
2
  def to_duration(options = {})
3
- ToDuration::Converter.call(to_i, options)
3
+ ToDuration::Duration.new(self).to_s(options)
4
4
  end
5
5
  end
data/lib/to_duration.rb CHANGED
@@ -1,6 +1,3 @@
1
1
  require 'to_duration/version'
2
- require 'to_duration/converter'
2
+ require 'to_duration/duration'
3
3
  require_relative 'numeric'
4
-
5
- module ToDuration
6
- end
@@ -0,0 +1,91 @@
1
+ module ToDuration
2
+ class Duration
3
+ UNITS = {
4
+ years: 315_576_00,
5
+ months: 259_200_0,
6
+ weeks: 604_800,
7
+ days: 864_00,
8
+ hours: 3600,
9
+ minutes: 60,
10
+ seconds: 1
11
+ }.freeze
12
+
13
+ attr_reader :seconds
14
+
15
+ def initialize(seconds)
16
+ @seconds = Integer(seconds)
17
+ end
18
+
19
+ def years
20
+ seconds / UNITS[:years]
21
+ end
22
+
23
+ def months
24
+ seconds / UNITS[:months]
25
+ end
26
+
27
+ def weeks
28
+ seconds / UNITS[:weeks]
29
+ end
30
+
31
+ def days
32
+ seconds / UNITS[:days]
33
+ end
34
+
35
+ def hours
36
+ seconds / UNITS[:hours]
37
+ end
38
+
39
+ def minutes
40
+ seconds / UNITS[:minutes]
41
+ end
42
+
43
+ def add(unit, count)
44
+ @seconds += UNITS[unit] * count
45
+ end
46
+
47
+ def sub(unit, count)
48
+ @seconds -= UNITS[unit] * count
49
+ end
50
+
51
+ def to_s(options = {})
52
+ return t('less_than_one_second') if seconds < 1
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
62
+
63
+ def t(key, options = {})
64
+ I18n.t("to_duration.#{key}", options)
65
+ end
66
+
67
+ def to_units(options)
68
+ duration = Duration.new(seconds)
69
+
70
+ {}.tap do |units|
71
+ UNITS.keys.each do |k|
72
+ next if options[k] != true
73
+
74
+ count = duration.public_send(k)
75
+ next if count.zero?
76
+
77
+ units[k] = count
78
+ duration.sub(k, count)
79
+ end
80
+ end
81
+ 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
+ end
91
+ end
@@ -1,3 +1,3 @@
1
1
  module ToDuration
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
data/to_duration.gemspec CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.13'
26
- spec.add_development_dependency 'rake', '~> 10.0'
27
- spec.add_development_dependency 'minitest', '~> 5.0'
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
28
26
  spec.add_development_dependency 'coveralls', '~> 0.8'
27
+ spec.add_development_dependency 'minitest', '~> 5.0'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikolay Digaev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-07 00:00:00.000000000 Z
11
+ date: 2018-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.13'
19
+ version: '1.16'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.13'
26
+ version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: coveralls
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0.8'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0.8'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: coveralls
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.8'
61
+ version: '10.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.8'
68
+ version: '10.0'
69
69
  description: A Ruby gem for converting seconds into human-readable format.
70
70
  email:
71
71
  - ffs.cmp@gmail.com
@@ -85,7 +85,7 @@ files:
85
85
  - bin/setup
86
86
  - lib/numeric.rb
87
87
  - lib/to_duration.rb
88
- - lib/to_duration/converter.rb
88
+ - lib/to_duration/duration.rb
89
89
  - lib/to_duration/version.rb
90
90
  - to_duration.gemspec
91
91
  homepage: https://github.com/digaev/to_duration
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.5.2
111
+ rubygems_version: 2.7.6
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: A Ruby gem for converting seconds into human-readable format.
@@ -1,61 +0,0 @@
1
- module ToDuration
2
- class Converter
3
- def self.call(value, options = {})
4
- new(value, options).call
5
- end
6
-
7
- def initialize(value, options = {})
8
- @value = value
9
- @options = DEFAULTS.dup.merge!(options).freeze
10
- end
11
-
12
- def call
13
- units = to_units
14
- last = if units.length > 1
15
- " #{I18n.t('to_duration.and')} #{units.pop}"
16
- else
17
- ''
18
- end
19
- "#{units.join(', ')}#{last}"
20
- end
21
-
22
- private
23
-
24
- UNITS = {
25
- year: 315_576_00,
26
- month: 259_200_0,
27
- week: 604_800,
28
- day: 864_00,
29
- hour: 3600,
30
- minute: 60,
31
- second: 1
32
- }.freeze
33
-
34
- DEFAULTS = {
35
- year: true,
36
- month: true,
37
- week: false,
38
- day: true,
39
- hour: true,
40
- minute: true,
41
- second: true
42
- }.freeze
43
-
44
- def to_units
45
- seconds = @value
46
-
47
- UNITS.map do |k, v|
48
- next if @options[k] != true
49
-
50
- count = seconds >= v ? seconds / v : 0
51
- seconds -= count * v
52
-
53
- t_unit(k, count) if count > 0
54
- end.compact
55
- end
56
-
57
- def t_unit(unit, count)
58
- "#{count} #{I18n.t("to_duration.#{unit}", count: count)}"
59
- end
60
- end
61
- end