ruby-duration 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/Gemfile.lock +14 -10
- data/lib/duration.rb +50 -19
- data/lib/duration/version.rb +2 -2
- data/test/test_duration.rb +70 -9
- metadata +60 -19
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm 1.9.
|
1
|
+
rvm 1.9.3@ruby-duration
|
data/Gemfile.lock
CHANGED
@@ -1,22 +1,26 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-duration (2.0
|
4
|
+
ruby-duration (2.1.0)
|
5
5
|
activesupport (>= 3.0.0)
|
6
6
|
i18n
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activesupport (3.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
activesupport (3.2.3)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
multi_json (~> 1.0)
|
14
|
+
bluecloth (2.2.0)
|
15
|
+
i18n (0.6.0)
|
16
|
+
minitest (2.12.0)
|
17
|
+
multi_json (1.2.0)
|
18
|
+
rake (0.9.2.2)
|
19
|
+
simplecov (0.6.1)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
simplecov-html (~> 0.5.3)
|
22
|
+
simplecov-html (0.5.3)
|
23
|
+
yard (0.7.5)
|
20
24
|
|
21
25
|
PLATFORMS
|
22
26
|
ruby
|
data/lib/duration.rb
CHANGED
@@ -71,6 +71,10 @@ class Duration
|
|
71
71
|
def %(other)
|
72
72
|
Duration.new(@total % other.to_i)
|
73
73
|
end
|
74
|
+
|
75
|
+
%w(minutes hours days).each do |meth|
|
76
|
+
define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
|
77
|
+
end
|
74
78
|
|
75
79
|
# Formats a duration in ISO8601.
|
76
80
|
# @see http://en.wikipedia.org/wiki/ISO_8601#Durations
|
@@ -101,20 +105,28 @@ class Duration
|
|
101
105
|
|
102
106
|
# Format a duration into a human-readable string.
|
103
107
|
#
|
104
|
-
# %w
|
105
|
-
# %d
|
106
|
-
# %h
|
107
|
-
# %m
|
108
|
-
# %s
|
109
|
-
# %
|
110
|
-
# %
|
111
|
-
# %
|
112
|
-
# %
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
# %~
|
108
|
+
# %w => weeks
|
109
|
+
# %d => days
|
110
|
+
# %h => hours
|
111
|
+
# %m => minutes
|
112
|
+
# %s => seconds
|
113
|
+
# %td => total days
|
114
|
+
# %th => total hours
|
115
|
+
# %tm => total minutes
|
116
|
+
# %ts => total seconds
|
117
|
+
# %t => total seconds
|
118
|
+
# %H => zero-padded hours
|
119
|
+
# %M => zero-padded minutes
|
120
|
+
# %S => zero-padded seconds
|
121
|
+
# %~s => locale-dependent "seconds" terminology
|
122
|
+
# %~m => locale-dependent "minutes" terminology
|
123
|
+
# %~h => locale-dependent "hours" terminology
|
124
|
+
# %~d => locale-dependent "days" terminology
|
125
|
+
# %~w => locale-dependent "weeks" terminology
|
126
|
+
# %tdu => total days with locale-dependent unit
|
127
|
+
# %thu => total hours with locale-dependent unit
|
128
|
+
# %tmu => total minutes with locale-dependent unit
|
129
|
+
# %tsu => total seconds with locale-dependent unit
|
118
130
|
#
|
119
131
|
# You can also use the I18n support.
|
120
132
|
# The %~s, %~m, %~h, %~d and %~w can be translated with I18n.
|
@@ -144,6 +156,10 @@ class Duration
|
|
144
156
|
'h' => @hours,
|
145
157
|
'm' => @minutes,
|
146
158
|
's' => @seconds,
|
159
|
+
'td' => Proc.new { total_days },
|
160
|
+
'th' => Proc.new { total_hours },
|
161
|
+
'tm' => Proc.new { total_minutes },
|
162
|
+
'ts' => @total,
|
147
163
|
't' => @total,
|
148
164
|
'H' => @hours.to_s.rjust(2, '0'),
|
149
165
|
'M' => @minutes.to_s.rjust(2, '0'),
|
@@ -152,11 +168,15 @@ class Duration
|
|
152
168
|
'~m' => i18n_for(:minute),
|
153
169
|
'~h' => i18n_for(:hour),
|
154
170
|
'~d' => i18n_for(:day),
|
155
|
-
'~w' => i18n_for(:week)
|
171
|
+
'~w' => i18n_for(:week),
|
172
|
+
'tdu'=> Proc.new { "#{total_days} #{i18n_for(:total_day)}"},
|
173
|
+
'thu'=> Proc.new { "#{total_hours} #{i18n_for(:total_hour)}"},
|
174
|
+
'tmu'=> Proc.new { "#{total_minutes} #{i18n_for(:total_minute)}"},
|
175
|
+
'tsu'=> Proc.new { "#{total} #{i18n_for(:total)}"}
|
156
176
|
}
|
157
177
|
|
158
|
-
format_str.gsub(/%?%(w|d|h|m|s|t
|
159
|
-
match['%%'] ? match : identifiers[match[1..-1]]
|
178
|
+
format_str.gsub(/%?%(w|d|h|m|s|t([dhms]u?)?|H|M|S|~(?:s|m|h|d|w))/) do |match|
|
179
|
+
match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
|
160
180
|
end.gsub('%%', '%')
|
161
181
|
end
|
162
182
|
|
@@ -183,8 +203,19 @@ private
|
|
183
203
|
end
|
184
204
|
|
185
205
|
def i18n_for(singular)
|
186
|
-
|
187
|
-
|
206
|
+
if singular == :total
|
207
|
+
fn_name = :total
|
208
|
+
singular = :second
|
209
|
+
plural = 'seconds'
|
210
|
+
elsif singular.to_s.start_with?('total_')
|
211
|
+
fn_name = "#{singular}s"
|
212
|
+
singular = singular.to_s['total_'.length..-1]
|
213
|
+
plural = "#{singular}s"
|
214
|
+
else
|
215
|
+
plural = "#{singular}s"
|
216
|
+
fn_name = plural
|
217
|
+
end
|
218
|
+
label = send(fn_name) == 1 ? singular : plural
|
188
219
|
|
189
220
|
I18n.t(label, :scope => :ruby_duration, :default => label.to_s)
|
190
221
|
end
|
data/lib/duration/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
class Duration
|
2
|
-
VERSION = "2.0
|
3
|
-
end
|
2
|
+
VERSION = "2.1.0"
|
3
|
+
end
|
data/test/test_duration.rb
CHANGED
@@ -10,16 +10,22 @@ describe "Duration" do
|
|
10
10
|
assert_equal 1, d.minutes
|
11
11
|
assert_equal 30, d.seconds
|
12
12
|
assert_equal 90, d.total
|
13
|
+
assert_equal 1, d.total_minutes
|
14
|
+
assert_equal 0, d.total_hours
|
15
|
+
assert_equal 0, d.total_days
|
13
16
|
end
|
14
17
|
|
15
18
|
it "should initialize given duration in Hash" do
|
16
19
|
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
|
17
|
-
assert_equal
|
18
|
-
assert_equal
|
19
|
-
assert_equal
|
20
|
-
assert_equal
|
21
|
-
assert_equal
|
20
|
+
assert_equal 1, d.weeks
|
21
|
+
assert_equal 2, d.days
|
22
|
+
assert_equal 3, d.hours
|
23
|
+
assert_equal 4, d.minutes
|
24
|
+
assert_equal 5, d.seconds
|
22
25
|
assert_equal 788645, d.total
|
26
|
+
assert_equal 13144, d.total_minutes
|
27
|
+
assert_equal 219, d.total_hours
|
28
|
+
assert_equal 9, d.total_days
|
23
29
|
end
|
24
30
|
|
25
31
|
describe "mathematical operations" do
|
@@ -29,22 +35,22 @@ describe "Duration" do
|
|
29
35
|
assert_equal Duration.new(15), Duration.new(10) + Duration.new(5)
|
30
36
|
end
|
31
37
|
|
32
|
-
it "
|
38
|
+
it "should -" do
|
33
39
|
assert_equal Duration.new(5), Duration.new(10) - 5
|
34
40
|
assert_equal Duration.new(5), Duration.new(10) - Duration.new(5)
|
35
41
|
end
|
36
42
|
|
37
|
-
it "
|
43
|
+
it "should *" do
|
38
44
|
assert_equal Duration.new(20), Duration.new(10) * 2
|
39
45
|
assert_equal Duration.new(20), Duration.new(10) * Duration.new(2)
|
40
46
|
end
|
41
47
|
|
42
|
-
it "
|
48
|
+
it "should /" do
|
43
49
|
assert_equal Duration.new(5), Duration.new(10) / 2
|
44
50
|
assert_equal Duration.new(5), Duration.new(10) / Duration.new(2)
|
45
51
|
end
|
46
52
|
|
47
|
-
it "
|
53
|
+
it "should %" do
|
48
54
|
assert_equal Duration.new(1), Duration.new(10) % 3
|
49
55
|
assert_equal Duration.new(1), Duration.new(10) % Duration.new(3)
|
50
56
|
end
|
@@ -60,6 +66,61 @@ describe "Duration" do
|
|
60
66
|
d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
|
61
67
|
assert_equal "1 week 1 day 1 hour 1 minute 1 second", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s")
|
62
68
|
end
|
69
|
+
|
70
|
+
it "should display total seconds" do
|
71
|
+
d = Duration.new(:hours => 1, :minutes => 15)
|
72
|
+
assert_equal "4500 seconds", d.format("%tsu")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should display total seconds in plural form when needed" do
|
76
|
+
d = Duration.new(:minutes => 1, :seconds => 1)
|
77
|
+
assert_equal "61 seconds", d.format("%tsu")
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should display total minutes as number" do
|
81
|
+
d = Duration.new(:hours => 1, :minutes => 15)
|
82
|
+
assert_equal "75", d.format("%tm")
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should display total minutes with unit' do
|
86
|
+
d = Duration.new(:hours => 1, :minutes => 15)
|
87
|
+
assert_equal "75 minutes", d.format("%tmu")
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should display total minutes in plural form when needed" do
|
91
|
+
d = Duration.new(:hours => 1, :minutes => 1)
|
92
|
+
assert_equal "61 minutes", d.format("%tmu")
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should display total hours as number" do
|
96
|
+
d = Duration.new(:days => 2, :hours => 1)
|
97
|
+
assert_equal "49", d.format("%th")
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should display total hours with unit' do
|
101
|
+
d = Duration.new(:days => 2, :hours => 2)
|
102
|
+
assert_equal "50 hours", d.format("%thu")
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should display total hours in plural form when needed" do
|
106
|
+
d = Duration.new(:days => 1, :hours => 1)
|
107
|
+
assert_equal "25 hours", d.format("%thu")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should display total days as number" do
|
111
|
+
d = Duration.new(:weeks => 1, :days => 3)
|
112
|
+
assert_equal "10", d.format("%td")
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should display total days with unit' do
|
116
|
+
d = Duration.new(:weeks => 1, :days => 2)
|
117
|
+
assert_equal "9 days", d.format("%tdu")
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should display total days in plural form when needed" do
|
121
|
+
d = Duration.new(:weeks => 1, :days => 1)
|
122
|
+
assert_equal "8 days", d.format("%tdu")
|
123
|
+
end
|
63
124
|
end
|
64
125
|
|
65
126
|
describe "#iso_6801" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2012-04-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: 3.0.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 3.0.0
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: i18n
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ! '>='
|
@@ -33,10 +38,15 @@ dependencies:
|
|
33
38
|
version: '0'
|
34
39
|
type: :runtime
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
37
47
|
- !ruby/object:Gem::Dependency
|
38
48
|
name: bundler
|
39
|
-
requirement:
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
51
|
requirements:
|
42
52
|
- - ! '>='
|
@@ -44,10 +54,15 @@ dependencies:
|
|
44
54
|
version: 1.0.0
|
45
55
|
type: :development
|
46
56
|
prerelease: false
|
47
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.0.0
|
48
63
|
- !ruby/object:Gem::Dependency
|
49
64
|
name: minitest
|
50
|
-
requirement:
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
51
66
|
none: false
|
52
67
|
requirements:
|
53
68
|
- - ! '>='
|
@@ -55,10 +70,15 @@ dependencies:
|
|
55
70
|
version: '0'
|
56
71
|
type: :development
|
57
72
|
prerelease: false
|
58
|
-
version_requirements:
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
59
79
|
- !ruby/object:Gem::Dependency
|
60
80
|
name: yard
|
61
|
-
requirement:
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
62
82
|
none: false
|
63
83
|
requirements:
|
64
84
|
- - ! '>='
|
@@ -66,10 +86,15 @@ dependencies:
|
|
66
86
|
version: '0'
|
67
87
|
type: :development
|
68
88
|
prerelease: false
|
69
|
-
version_requirements:
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
70
95
|
- !ruby/object:Gem::Dependency
|
71
96
|
name: rake
|
72
|
-
requirement:
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
73
98
|
none: false
|
74
99
|
requirements:
|
75
100
|
- - ! '>='
|
@@ -77,10 +102,15 @@ dependencies:
|
|
77
102
|
version: '0'
|
78
103
|
type: :development
|
79
104
|
prerelease: false
|
80
|
-
version_requirements:
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
81
111
|
- !ruby/object:Gem::Dependency
|
82
112
|
name: simplecov
|
83
|
-
requirement:
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
84
114
|
none: false
|
85
115
|
requirements:
|
86
116
|
- - ! '>='
|
@@ -88,10 +118,15 @@ dependencies:
|
|
88
118
|
version: 0.3.5
|
89
119
|
type: :development
|
90
120
|
prerelease: false
|
91
|
-
version_requirements:
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 0.3.5
|
92
127
|
- !ruby/object:Gem::Dependency
|
93
128
|
name: bluecloth
|
94
|
-
requirement:
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
95
130
|
none: false
|
96
131
|
requirements:
|
97
132
|
- - ! '>='
|
@@ -99,7 +134,12 @@ dependencies:
|
|
99
134
|
version: 0.3.5
|
100
135
|
type: :development
|
101
136
|
prerelease: false
|
102
|
-
version_requirements:
|
137
|
+
version_requirements: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 0.3.5
|
103
143
|
description: Duration type
|
104
144
|
email:
|
105
145
|
- jose@peleteiro.net
|
@@ -146,8 +186,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
186
|
version: 1.3.6
|
147
187
|
requirements: []
|
148
188
|
rubyforge_project: ruby-duration
|
149
|
-
rubygems_version: 1.8.
|
189
|
+
rubygems_version: 1.8.18
|
150
190
|
signing_key:
|
151
191
|
specification_version: 3
|
152
192
|
summary: Duration type
|
153
193
|
test_files: []
|
194
|
+
has_rdoc:
|