time_units 0.1.0 → 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.
- data/ChangeLog +7 -0
- data/lib/time_units/duration.rb +24 -14
- data/lib/time_units/version.rb +1 -1
- data/specs/unit/duration_spec.rb +4 -0
- metadata +5 -4
data/ChangeLog
ADDED
data/lib/time_units/duration.rb
CHANGED
@@ -5,21 +5,25 @@ module TimeDuration
|
|
5
5
|
end
|
6
6
|
|
7
7
|
self.units = [
|
8
|
-
[1.
|
9
|
-
[1.
|
10
|
-
[1.
|
11
|
-
[1.
|
12
|
-
[1,
|
8
|
+
[1.year, :years, "year", "years"],
|
9
|
+
[1.month, :months, "month", "months"],
|
10
|
+
[1.week, :weeks, "week", "weeks"],
|
11
|
+
[1.day, :days, "day", "days"],
|
12
|
+
[1.hour, :hours, "hour", "hours"],
|
13
|
+
[1.minute, :minutes, "min", "mins"],
|
14
|
+
[1, :seconds, "s", "s"]
|
13
15
|
]
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
##
|
18
|
+
# Format a duration for display.
|
19
|
+
#
|
20
|
+
# @param [Integer,nil] limit if set this will limit the
|
21
|
+
# number of fields shown (ex: 2 = 2 months 3 weeks)
|
22
|
+
#
|
23
|
+
# @return [String] the formatted duration
|
24
|
+
#
|
25
|
+
def human_duration(limit = nil)
|
26
|
+
diff = Hash.new(0)
|
23
27
|
|
24
28
|
ret = []
|
25
29
|
|
@@ -45,7 +49,13 @@ module TimeDuration
|
|
45
49
|
|
46
50
|
TimeDuration.units.each.with_index do |unit|
|
47
51
|
unit_name = unit[1]
|
48
|
-
|
52
|
+
if diff[unit_name] > 0
|
53
|
+
ret << pluralize.(diff[unit_name], unit)
|
54
|
+
|
55
|
+
if ret.size == limit
|
56
|
+
break
|
57
|
+
end
|
58
|
+
end
|
49
59
|
end
|
50
60
|
|
51
61
|
ret.join(' ')
|
data/lib/time_units/version.rb
CHANGED
data/specs/unit/duration_spec.rb
CHANGED
@@ -18,5 +18,9 @@ describe 'TimeDuration' do
|
|
18
18
|
(2*day1 + 21*hour1 + 3).human_duration.should == "2 days 21 hours 3 s"
|
19
19
|
|
20
20
|
(2.weeks + 36.hours).human_duration.should == "2 weeks 1 day 12 hours"
|
21
|
+
(2.weeks + 36.hours).human_duration(2).should == "2 weeks 1 day"
|
22
|
+
|
23
|
+
(2.months).human_duration.should == "2 months"
|
24
|
+
(2.years + 1.month).human_duration.should == "2 years 1 month"
|
21
25
|
end
|
22
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_units
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- ChangeLog
|
22
23
|
- Gemfile
|
23
24
|
- Guardfile
|
24
25
|
- LICENSE
|
@@ -46,7 +47,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
47
|
version: '0'
|
47
48
|
segments:
|
48
49
|
- 0
|
49
|
-
hash:
|
50
|
+
hash: 4060331108915470852
|
50
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
52
|
none: false
|
52
53
|
requirements:
|
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
version: '0'
|
56
57
|
segments:
|
57
58
|
- 0
|
58
|
-
hash:
|
59
|
+
hash: 4060331108915470852
|
59
60
|
requirements: []
|
60
61
|
rubyforge_project:
|
61
62
|
rubygems_version: 1.8.24
|