ruby-duration 0.2.2 → 0.3.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/README.md +1 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/duration.rb +21 -1
- data/ruby-duration.gemspec +3 -3
- data/test/test_duration.rb +39 -11
- metadata +5 -5
data/README.md
CHANGED
@@ -4,8 +4,7 @@ ruby-duration
|
|
4
4
|
Duration is an immutable type that represents some amount of time with accuracy in seconds.
|
5
5
|
|
6
6
|
A lot of the code and inspirations is borrowed from [duration](http://rubyforge.org/projects/duration)
|
7
|
-
lib, which is a **mutable** Duration type
|
8
|
-
a new one because and belive it's better to be immutable and the old project is still on rubyforge's subversion.
|
7
|
+
lib, which is a **mutable** Duration type with lot more features but lack of tests.
|
9
8
|
|
10
9
|
I'll try to contact the author of duration to make ruby-duration version 2 of duration's lib. ;)
|
11
10
|
|
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ begin
|
|
8
8
|
gem.summary = %Q{Duration type}
|
9
9
|
gem.description = %Q{Duration type}
|
10
10
|
gem.email = "jose@peleteiro.net"
|
11
|
-
gem.homepage = "http://github.com/peleteiro/duration"
|
11
|
+
gem.homepage = "http://github.com/peleteiro/ruby-duration"
|
12
12
|
gem.authors = ["Jose Peleteiro"]
|
13
13
|
gem.add_development_dependency "minitest", ">= 0"
|
14
14
|
gem.add_development_dependency "yard", ">= 0"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/duration.rb
CHANGED
@@ -60,7 +60,27 @@ class Duration
|
|
60
60
|
return false unless other.is_a?(Duration)
|
61
61
|
@total <=> other.to_i
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
|
+
def +(other)
|
65
|
+
Duration.new(@total + other.to_i)
|
66
|
+
end
|
67
|
+
|
68
|
+
def -(other)
|
69
|
+
Duration.new(@total - other.to_i)
|
70
|
+
end
|
71
|
+
|
72
|
+
def *(other)
|
73
|
+
Duration.new(@total * other.to_i)
|
74
|
+
end
|
75
|
+
|
76
|
+
def /(other)
|
77
|
+
Duration.new(@total / other.to_i)
|
78
|
+
end
|
79
|
+
|
80
|
+
def %(other)
|
81
|
+
Duration.new(@total % other.to_i)
|
82
|
+
end
|
83
|
+
|
64
84
|
# Formats a duration in ISO8601.
|
65
85
|
# @see http://en.wikipedia.org/wiki/ISO_8601#Durations
|
66
86
|
def iso8601
|
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.
|
8
|
+
s.version = "0.3.0"
|
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-22}
|
13
13
|
s.description = %q{Duration type}
|
14
14
|
s.email = %q{jose@peleteiro.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"test/helper.rb",
|
31
31
|
"test/test_duration.rb"
|
32
32
|
]
|
33
|
-
s.homepage = %q{http://github.com/peleteiro/duration}
|
33
|
+
s.homepage = %q{http://github.com/peleteiro/ruby-duration}
|
34
34
|
s.rdoc_options = ["--charset=UTF-8"]
|
35
35
|
s.require_paths = ["lib"]
|
36
36
|
s.rubygems_version = %q{1.3.7}
|
data/test/test_duration.rb
CHANGED
@@ -22,57 +22,85 @@ describe "Duration" do
|
|
22
22
|
assert_equal 5, d.seconds
|
23
23
|
assert_equal 788645, d.total
|
24
24
|
end
|
25
|
+
|
26
|
+
describe "mathematical operations" do
|
27
|
+
|
28
|
+
it "should +" do
|
29
|
+
assert_equal Duration.new(15), Duration.new(10) + 5
|
30
|
+
assert_equal Duration.new(15), Duration.new(10) + Duration.new(5)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "sould -" do
|
34
|
+
assert_equal Duration.new(5), Duration.new(10) - 5
|
35
|
+
assert_equal Duration.new(5), Duration.new(10) - Duration.new(5)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "sould *" do
|
39
|
+
assert_equal Duration.new(20), Duration.new(10) * 2
|
40
|
+
assert_equal Duration.new(20), Duration.new(10) * Duration.new(2)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "sould /" do
|
44
|
+
assert_equal Duration.new(5), Duration.new(10) / 2
|
45
|
+
assert_equal Duration.new(5), Duration.new(10) / Duration.new(2)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "sould %" do
|
49
|
+
assert_equal Duration.new(1), Duration.new(10) % 3
|
50
|
+
assert_equal Duration.new(1), Duration.new(10) % Duration.new(3)
|
51
|
+
end
|
52
|
+
end
|
25
53
|
|
26
54
|
describe "#format" do
|
27
55
|
it "should display units in plural form when needed" do
|
28
56
|
d = Duration.new(:weeks => 2, :days => 3, :hours => 4, :minutes => 5, :seconds => 6)
|
29
|
-
assert_equal
|
57
|
+
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
58
|
end
|
31
59
|
|
32
60
|
it "should display units in singular form when needed" do
|
33
61
|
d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
|
34
|
-
assert_equal
|
62
|
+
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
63
|
end
|
36
64
|
end
|
37
65
|
|
38
66
|
describe "#iso_6801" do
|
39
67
|
it "should format seconds" do
|
40
68
|
d = Duration.new(:seconds => 1)
|
41
|
-
assert_equal
|
69
|
+
assert_equal "PT1S", d.iso8601
|
42
70
|
end
|
43
71
|
|
44
72
|
it "should format minutes" do
|
45
73
|
d = Duration.new(:minutes => 1)
|
46
|
-
assert_equal
|
74
|
+
assert_equal "PT1M", d.iso8601
|
47
75
|
end
|
48
76
|
|
49
77
|
it "should format hours" do
|
50
78
|
d = Duration.new(:hours => 1)
|
51
|
-
assert_equal
|
79
|
+
assert_equal "PT1H", d.iso8601
|
52
80
|
end
|
53
81
|
|
54
82
|
it "should format days" do
|
55
83
|
d = Duration.new(:days => 1)
|
56
|
-
assert_equal
|
84
|
+
assert_equal "P1D", d.iso8601
|
57
85
|
end
|
58
86
|
|
59
87
|
it "should format weeks" do
|
60
88
|
d = Duration.new(:weeks => 1)
|
61
|
-
assert_equal
|
89
|
+
assert_equal "P1W", d.iso8601
|
62
90
|
end
|
63
91
|
|
64
92
|
it "should format only with given values" do
|
65
93
|
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
|
66
|
-
assert_equal
|
94
|
+
assert_equal "P1W2DT3H4M5S", d.iso8601
|
67
95
|
|
68
96
|
d = Duration.new(:weeks => 1, :hours => 2, :seconds => 3)
|
69
|
-
assert_equal
|
97
|
+
assert_equal "P1WT2H3S", d.iso8601
|
70
98
|
|
71
99
|
d = Duration.new(:weeks => 1, :days => 2)
|
72
|
-
assert_equal
|
100
|
+
assert_equal "P1W2D", d.iso8601
|
73
101
|
|
74
102
|
d = Duration.new(:hours => 1, :seconds => 30)
|
75
|
-
assert_equal
|
103
|
+
assert_equal "PT1H30S", d.iso8601
|
76
104
|
end
|
77
105
|
|
78
106
|
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
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-22 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -69,7 +69,7 @@ files:
|
|
69
69
|
- test/helper.rb
|
70
70
|
- test/test_duration.rb
|
71
71
|
has_rdoc: true
|
72
|
-
homepage: http://github.com/peleteiro/duration
|
72
|
+
homepage: http://github.com/peleteiro/ruby-duration
|
73
73
|
licenses: []
|
74
74
|
|
75
75
|
post_install_message:
|