time_duration_humanizer 0.1.6 → 2.0.0

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
  SHA1:
3
- metadata.gz: c9ea67a4041178b0a9d2e17f55f53b9ca04e8fa6
4
- data.tar.gz: 5174c75e85618f1c997978db790f402efb0ce9df
3
+ metadata.gz: 6dc5cfeb08e314bd679827105e34469f282f551d
4
+ data.tar.gz: 69ddae6813267280ba8eca00bec1213428385513
5
5
  SHA512:
6
- metadata.gz: 65789569eeb769d3ffe6ee4a5155b338d083a5364d5535dba54eeaa5881b328157712feca25a9522d3b7adfdb230f4d053293d3c778fdd3c46569d129ef0f610
7
- data.tar.gz: e29a7baa0d550aef8b3d2286396d5207c3d4aa140847ffee0643c5fcaa176ac334d4f72a9fd8782edfd4e4080427da1acb692ef760da83525ccdb5a8c8b930f8
6
+ metadata.gz: 433d18188f3a68944ffb9727847665c2e1c096c8e9ee8e96d936612d21c073bbc94a0e4c699e9e2dc8c5c1ecc61724981b0b529950fee07e4801c218d58768ca
7
+ data.tar.gz: dd1d1f12ce0e547e8de922b117f1f87358c64287b7e4aff1cad001b998e6472a46cb828881d6ed8b618dad4d1279c98263b10ed462cbebeaf7f3b3f0957abbd4
data/README.md CHANGED
@@ -23,39 +23,36 @@ Or install it yourself as:
23
23
  ## Usage
24
24
 
25
25
  ```ruby
26
- irb(main):001:0> TimeDurationHumanizer.humanize(12345)
26
+ irb(main):001:0> 12345.to_duration
27
27
  => "3 hours, 25 minutes and 45 seconds"
28
28
 
29
- irb(main):002:0> TimeDurationHumanizer.humanize(12345, { and_at_end: false })
29
+ irb(main):002:0> 12345.to_duration(and_at_end: false)
30
30
  => "3 hours, 25 minutes, 45 seconds"
31
31
 
32
- irb(main):003:0> TimeDurationHumanizer.humanize(1234567890)
32
+ irb(main):003:0> 1234567890.to_duration
33
33
  => "39 years, 1 month, 14 days, 5 hours, 31 minutes and 30 seconds"
34
34
 
35
- irb(main):004:0> TimeDurationHumanizer.humanize(1234567890, {}, { weeks: true })
35
+ irb(main):004:0> 1234567890.to_duration(weeks: true)
36
36
  => "39 years, 1 month, 2 weeks, 5 hours, 31 minutes and 30 seconds"
37
37
 
38
- irb(main):005:0> TimeDurationHumanizer.humanize(62208000)
38
+ irb(main):005:0> 62208000.to_duration
39
39
  => "1 year, 11 months, 24 days and 18 hours"
40
40
 
41
- irb(main):006:0> TimeDurationHumanizer.humanize(62208000, { days_in_year: 360 })
41
+ irb(main):006:0> 62208000.to_duration(days_in_year: 360)
42
42
  => "2 years"
43
43
  ```
44
44
 
45
- ## Options (second parameter)
45
+ ## Options
46
46
 
47
- * and_at_end - default `true`
48
- * days_in_year - default `365.25`
49
-
50
- ## Units (third parameter)
51
-
52
- * years - default `true`
53
- * months - default `true`
54
- * weeks - default `false`
55
- * days - default `true`
56
- * hours - default `true`
57
- * minutes - default `true`
58
- * seconds - default `true`
47
+ * `and_at_end` - default `true`
48
+ * `days_in_year` - default `365.25`
49
+ * `year` - default `true`
50
+ * `month` - default `true`
51
+ * `week` - default `false`
52
+ * `day` - default `true`
53
+ * `hour` - default `true`
54
+ * `minute` - default `true`
55
+ * `second` - default `true`
59
56
 
60
57
  ## Localization
61
58
 
@@ -65,20 +62,27 @@ In your Rails application edit `config/locales/en.yml`:
65
62
  en:
66
63
  time_duration_humanizer:
67
64
  and: and
68
- year: year
69
- years: years
70
- month: month
71
- months: months
72
- week: week
73
- weeks: weeks
74
- day: day
75
- days: days
76
- hour: hour
77
- hours: hours
78
- minute: minute
79
- minutes: minutes
80
- second: second
81
- seconds: seconds
65
+ year:
66
+ one: year
67
+ other: years
68
+ month:
69
+ one: month
70
+ other: months
71
+ week:
72
+ one: week
73
+ other: weeks
74
+ day:
75
+ one: day
76
+ other: days
77
+ hour:
78
+ one: hour
79
+ other: hours
80
+ minute:
81
+ one: minute
82
+ other: minutes
83
+ second:
84
+ one: second
85
+ other: seconds
82
86
  ```
83
87
 
84
88
  ## Contributing
@@ -1,52 +1,61 @@
1
- require "time_duration_humanizer/version"
2
-
3
- module TimeDurationHumanizer
4
- @@units = {
5
- years: 31557600,
6
- months: 2592000,
7
- weeks: 604800,
8
- days: 86400,
9
- hours: 3600,
10
- minutes: 60,
11
- seconds: 1
12
- }
13
-
14
- def self.humanize(seconds, options = {}, units = {})
15
- options = {
16
- and_at_end: true,
17
- days_in_year: 365.25
18
- }.merge!(options)
19
-
20
- units = {
21
- years: true,
22
- months: true,
23
- weeks: false,
24
- days: true,
25
- hours: true,
26
- minutes: true,
27
- seconds: true
28
- }.merge!(units)
29
-
30
- values = []
31
- duration = ''
32
-
33
- units.each do |k, v|
34
- if v == true && @@units.keys.include?(k)
35
- u = k == :years ? (options[:days_in_year] * @@units[:days]).to_i : @@units[k]
36
- value = seconds >= u ? seconds / u : 0
37
- if value > 0
38
- values << { name: k.to_s, value: value }
39
- seconds -= value * u
40
- end
41
- end
42
- end
1
+ require 'time_duration_humanizer/version'
2
+
3
+ class Numeric
4
+ UNITS = {
5
+ year: 315_576_00,
6
+ month: 259_200_0,
7
+ week: 604_800,
8
+ day: 864_00,
9
+ hour: 3600,
10
+ minute: 60,
11
+ second: 1
12
+ }.freeze
13
+
14
+ DEFAULTS = {
15
+ and_at_end: true,
16
+ days_in_year: 365.25,
17
+ year: true,
18
+ month: true,
19
+ week: false,
20
+ day: true,
21
+ hour: true,
22
+ minute: true,
23
+ second: true
24
+ }.freeze
25
+
26
+ def to_duration(options = {})
27
+ options = DEFAULTS.dup.merge!(options)
28
+ units = to_duration_units(options).map { |v| t_duration_unit(v) }
29
+ last = if units.length > 1 && options[:and_at_end] == true
30
+ " #{I18n.t('time_duration_humanizer.and')} #{units.pop}"
31
+ else
32
+ ''
33
+ end
34
+ units.join(', ') + last
35
+ end
43
36
 
44
- l = values.length
45
- values.each_with_index do |v, i|
46
- separator = i == l - 1 ? '' : (i == l - 2 && options[:and_at_end] == true ? " #{I18n.t('time_duration_humanizer.and')} " : ', ')
47
- duration += "#{v[:value]} #{I18n.t("time_duration_humanizer.#{v[:value] == 1 ? v[:name].chop : v[:name]}")}#{separator}"
37
+ private
38
+
39
+ def to_duration_units(options)
40
+ units = []
41
+ seconds = to_i
42
+
43
+ UNITS.each do |k, v|
44
+ next if options[k] != true
45
+
46
+ secs = k == :year ? (options[:days_in_year] * UNITS[:day]).to_i : v
47
+ value = seconds >= secs ? seconds / secs : 0
48
+ if value > 0
49
+ units << { name: k, value: value }
50
+ seconds -= value * secs
51
+ end
48
52
  end
53
+ units
54
+ end
49
55
 
50
- duration
56
+ def t_duration_unit(unit)
57
+ "#{unit[:value]} " + I18n.t(
58
+ "time_duration_humanizer.#{unit[:name]}", count: unit[:value]
59
+ )
51
60
  end
52
61
  end
@@ -1,3 +1,3 @@
1
1
  module TimeDurationHumanizer
2
- VERSION = "0.1.6"
2
+ VERSION = '2.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_duration_humanizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 2.0.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: 2015-12-14 00:00:00.000000000 Z
11
+ date: 2016-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  version: '0'
78
78
  requirements: []
79
79
  rubyforge_project:
80
- rubygems_version: 2.4.5.1
80
+ rubygems_version: 2.5.1
81
81
  signing_key:
82
82
  specification_version: 4
83
83
  summary: A Ruby gem for converting seconds into human-readable format.