ruby-duration 0.2.0 → 0.2.1
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/VERSION +1 -1
- data/lib/duration.rb +17 -2
- data/ruby-duration.gemspec +2 -2
- data/test/duration/test_mongoid.rb +32 -29
- data/test/helper.rb +1 -4
- data/test/test_duration.rb +68 -27
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/duration.rb
CHANGED
@@ -64,9 +64,24 @@ class Duration
|
|
64
64
|
# Formats a duration in ISO8601.
|
65
65
|
# @see http://en.wikipedia.org/wiki/ISO_8601#Durations
|
66
66
|
def iso8601
|
67
|
-
|
67
|
+
output = ''
|
68
|
+
|
69
|
+
if days > 0 || weeks > 0
|
70
|
+
output << 'P'
|
71
|
+
output << "#{weeks}W" if weeks > 0
|
72
|
+
output << "#{days}D" if days > 0
|
73
|
+
end
|
74
|
+
|
75
|
+
if seconds > 0 || minutes > 0 || hours > 0
|
76
|
+
output << 'T'
|
77
|
+
output << "#{hours}H" if hours > 0
|
78
|
+
output << "#{minutes}M" if minutes > 0
|
79
|
+
output << "#{seconds}S" if seconds > 0
|
80
|
+
end
|
81
|
+
|
82
|
+
output
|
68
83
|
end
|
69
|
-
|
84
|
+
|
70
85
|
# @return true if total is 0
|
71
86
|
def blank?
|
72
87
|
@total == 0
|
data/ruby-duration.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ruby-duration}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jose Peleteiro"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-20}
|
13
13
|
s.description = %q{Duration type}
|
14
14
|
s.email = %q{jose@peleteiro.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -1,42 +1,45 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'helper'
|
3
3
|
|
4
|
-
|
4
|
+
describe "mongoid support" do
|
5
5
|
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def test_get_nil
|
12
|
-
assert_nil Duration.get(nil)
|
13
|
-
end
|
6
|
+
describe "#get - Mongoid's deserialization" do
|
7
|
+
it "should return the duration given the total in seconds" do
|
8
|
+
assert_equal Duration.new(90), Duration.get(90)
|
9
|
+
end
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
assert_nil Duration.set("")
|
19
|
-
assert_nil Duration.set({})
|
20
|
-
assert_nil Duration.set({:seconds => "", :hours => ""})
|
21
|
-
assert_nil Duration.set({:x => 100, :seconds => ""})
|
11
|
+
it "should return nil for nil serialized values" do
|
12
|
+
assert_nil Duration.get(nil)
|
13
|
+
end
|
22
14
|
end
|
23
15
|
|
24
|
-
|
25
|
-
|
26
|
-
|
16
|
+
describe "#set - Mongoid's serialization" do
|
17
|
+
it "should serialize to nil given an invalid serialized value or nil" do
|
18
|
+
assert_nil Duration.set([1,2,3])
|
19
|
+
assert_nil Duration.set(nil)
|
20
|
+
assert_nil Duration.set("")
|
21
|
+
assert_nil Duration.set({})
|
22
|
+
assert_nil Duration.set({:seconds => "", :hours => ""})
|
23
|
+
assert_nil Duration.set({:x => 100, :seconds => ""})
|
24
|
+
end
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
it "should return total seconds given a duration" do
|
27
|
+
assert_equal 90, Duration.set(Duration.new(:minutes => 1, :seconds => 30))
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return total seconds given a duration in seconds" do
|
31
|
+
assert_equal 10, Duration.set(10)
|
32
|
+
end
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
it "should return total seconds given a duration in string" do
|
35
|
+
assert_equal 10, Duration.set("10")
|
36
|
+
assert_equal 10, Duration.set("10string")
|
37
|
+
assert_equal 0, Duration.set("string") # not blank
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
+
it "should return total seconds given a duration in hash" do
|
41
|
+
assert_equal 90, Duration.set(:minutes => 1, :seconds => 30)
|
42
|
+
end
|
40
43
|
end
|
41
44
|
|
42
45
|
end
|
data/test/helper.rb
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'rubygems'
|
3
|
-
require 'minitest/
|
3
|
+
require 'minitest/spec'
|
4
4
|
|
5
5
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
7
|
require 'duration'
|
8
8
|
require 'duration/mongoid'
|
9
9
|
|
10
|
-
class MiniTest::Unit::TestCase
|
11
|
-
end
|
12
|
-
|
13
10
|
MiniTest::Unit.autorun
|
data/test/test_duration.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'helper'
|
3
3
|
|
4
|
-
|
4
|
+
describe "Duration" do
|
5
5
|
|
6
|
-
|
6
|
+
it "should initialize given duration in seconds" do
|
7
7
|
d = Duration.new(90)
|
8
8
|
assert_equal 0, d.weeks
|
9
9
|
assert_equal 0, d.days
|
@@ -13,39 +13,80 @@ class TestDuration < MiniTest::Unit::TestCase
|
|
13
13
|
assert_equal 90, d.total
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
d = Duration.new(:
|
18
|
-
assert_equal
|
19
|
-
assert_equal
|
20
|
-
assert_equal
|
21
|
-
assert_equal
|
22
|
-
assert_equal
|
23
|
-
assert_equal
|
16
|
+
it "should initialize given duration in Hash" do
|
17
|
+
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
|
18
|
+
assert_equal 1, d.weeks
|
19
|
+
assert_equal 2, d.days
|
20
|
+
assert_equal 3, d.hours
|
21
|
+
assert_equal 4, d.minutes
|
22
|
+
assert_equal 5, d.seconds
|
23
|
+
assert_equal 788645, d.total
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
describe "#format" do
|
27
|
+
it "should display units in plural form when needed" do
|
28
|
+
d = Duration.new(:weeks => 2, :days => 3, :hours => 4, :minutes => 5, :seconds => 6)
|
29
|
+
assert_equal("2 weeks 3 days 4 hours 5 minutes 6 seconds", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s"))
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
it "should display units in singular form when needed" do
|
33
|
+
d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
|
34
|
+
assert_equal("1 week 1 day 1 hour 1 minute 1 second", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s"))
|
35
|
+
end
|
34
36
|
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
describe "#iso_6801" do
|
39
|
+
it "should format seconds" do
|
40
|
+
d = Duration.new(:seconds => 1)
|
41
|
+
assert_equal("T1S", d.iso8601)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should format minutes" do
|
45
|
+
d = Duration.new(:minutes => 1)
|
46
|
+
assert_equal("T1M", d.iso8601)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should format hours" do
|
50
|
+
d = Duration.new(:hours => 1)
|
51
|
+
assert_equal("T1H", d.iso8601)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should format days" do
|
55
|
+
d = Duration.new(:days => 1)
|
56
|
+
assert_equal("P1D", d.iso8601)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should format weeks" do
|
60
|
+
d = Duration.new(:weeks => 1)
|
61
|
+
assert_equal("P1W", d.iso8601)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should format only with given values" do
|
65
|
+
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
|
66
|
+
assert_equal("P1W2DT3H4M5S", d.iso8601)
|
67
|
+
|
68
|
+
d = Duration.new(:weeks => 1, :hours => 2, :seconds => 3)
|
69
|
+
assert_equal("P1WT2H3S", d.iso8601)
|
70
|
+
|
71
|
+
d = Duration.new(:weeks => 1, :days => 2)
|
72
|
+
assert_equal("P1W2D", d.iso8601)
|
73
|
+
|
74
|
+
d = Duration.new(:hours => 1, :seconds => 30)
|
75
|
+
assert_equal("T1H30S", d.iso8601)
|
76
|
+
end
|
77
|
+
|
39
78
|
end
|
40
79
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
80
|
+
describe "utilities methods" do
|
81
|
+
it "should respond to blank?" do
|
82
|
+
assert Duration.new.blank?
|
83
|
+
refute Duration.new(1).blank?
|
84
|
+
end
|
45
85
|
|
46
|
-
|
47
|
-
|
48
|
-
|
86
|
+
it "should respond to present?" do
|
87
|
+
refute Duration.new.present?
|
88
|
+
assert Duration.new(1).present?
|
89
|
+
end
|
49
90
|
end
|
50
91
|
|
51
92
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jose Peleteiro
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-20 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|