Permalink
Browse files

mathematical operations

  • Loading branch information...
1 parent 14c4fa0 commit 22bc40ad3e4570d64cacaa351360cb720fa6cce9 @peleteiro committed Sep 23, 2010
Showing with 65 additions and 17 deletions.
  1. +1 −1 Rakefile
  2. +1 −1 VERSION
  3. +21 −1 lib/duration.rb
  4. +3 −3 ruby-duration.gemspec
  5. +39 −11 test/test_duration.rb
View
@@ -8,7 +8,7 @@ begin
gem.summary = %Q{Duration type}
gem.description = %Q{Duration type}
gem.email = "jose@peleteiro.net"
- gem.homepage = "http://github.com/peleteiro/duration"
+ gem.homepage = "http://github.com/peleteiro/ruby-duration"
gem.authors = ["Jose Peleteiro"]
gem.add_development_dependency "minitest", ">= 0"
gem.add_development_dependency "yard", ">= 0"
View
@@ -1 +1 @@
-0.2.2
+0.3.0
View
@@ -60,7 +60,27 @@ def <=>(other)
return false unless other.is_a?(Duration)
@total <=> other.to_i
end
-
+
+ def +(other)
+ Duration.new(@total + other.to_i)
+ end
+
+ def -(other)
+ Duration.new(@total - other.to_i)
+ end
+
+ def *(other)
+ Duration.new(@total * other.to_i)
+ end
+
+ def /(other)
+ Duration.new(@total / other.to_i)
+ end
+
+ def %(other)
+ Duration.new(@total % other.to_i)
+ end
+
# Formats a duration in ISO8601.
# @see http://en.wikipedia.org/wiki/ISO_8601#Durations
def iso8601
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{ruby-duration}
- s.version = "0.2.2"
+ s.version = "0.3.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jose Peleteiro"]
- s.date = %q{2010-09-20}
+ s.date = %q{2010-09-22}
s.description = %q{Duration type}
s.email = %q{jose@peleteiro.net}
s.extra_rdoc_files = [
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
"test/helper.rb",
"test/test_duration.rb"
]
- s.homepage = %q{http://github.com/peleteiro/duration}
+ s.homepage = %q{http://github.com/peleteiro/ruby-duration}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
View
@@ -22,57 +22,85 @@
assert_equal 5, d.seconds
assert_equal 788645, d.total
end
+
+ describe "mathematical operations" do
+
+ it "should +" do
+ assert_equal Duration.new(15), Duration.new(10) + 5
+ assert_equal Duration.new(15), Duration.new(10) + Duration.new(5)
+ end
+
+ it "sould -" do
+ assert_equal Duration.new(5), Duration.new(10) - 5
+ assert_equal Duration.new(5), Duration.new(10) - Duration.new(5)
+ end
+
+ it "sould *" do
+ assert_equal Duration.new(20), Duration.new(10) * 2
+ assert_equal Duration.new(20), Duration.new(10) * Duration.new(2)
+ end
+
+ it "sould /" do
+ assert_equal Duration.new(5), Duration.new(10) / 2
+ assert_equal Duration.new(5), Duration.new(10) / Duration.new(2)
+ end
+
+ it "sould %" do
+ assert_equal Duration.new(1), Duration.new(10) % 3
+ assert_equal Duration.new(1), Duration.new(10) % Duration.new(3)
+ end
+ end
describe "#format" do
it "should display units in plural form when needed" do
d = Duration.new(:weeks => 2, :days => 3, :hours => 4, :minutes => 5, :seconds => 6)
- assert_equal("2 weeks 3 days 4 hours 5 minutes 6 seconds", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s"))
+ assert_equal "2 weeks 3 days 4 hours 5 minutes 6 seconds", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s")
end
it "should display units in singular form when needed" do
d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
- assert_equal("1 week 1 day 1 hour 1 minute 1 second", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s"))
+ assert_equal "1 week 1 day 1 hour 1 minute 1 second", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s")
end
end
describe "#iso_6801" do
it "should format seconds" do
d = Duration.new(:seconds => 1)
- assert_equal("PT1S", d.iso8601)
+ assert_equal "PT1S", d.iso8601
end
it "should format minutes" do
d = Duration.new(:minutes => 1)
- assert_equal("PT1M", d.iso8601)
+ assert_equal "PT1M", d.iso8601
end
it "should format hours" do
d = Duration.new(:hours => 1)
- assert_equal("PT1H", d.iso8601)
+ assert_equal "PT1H", d.iso8601
end
it "should format days" do
d = Duration.new(:days => 1)
- assert_equal("P1D", d.iso8601)
+ assert_equal "P1D", d.iso8601
end
it "should format weeks" do
d = Duration.new(:weeks => 1)
- assert_equal("P1W", d.iso8601)
+ assert_equal "P1W", d.iso8601
end
it "should format only with given values" do
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
- assert_equal("P1W2DT3H4M5S", d.iso8601)
+ assert_equal "P1W2DT3H4M5S", d.iso8601
d = Duration.new(:weeks => 1, :hours => 2, :seconds => 3)
- assert_equal("P1WT2H3S", d.iso8601)
+ assert_equal "P1WT2H3S", d.iso8601
d = Duration.new(:weeks => 1, :days => 2)
- assert_equal("P1W2D", d.iso8601)
+ assert_equal "P1W2D", d.iso8601
d = Duration.new(:hours => 1, :seconds => 30)
- assert_equal("PT1H30S", d.iso8601)
+ assert_equal "PT1H30S", d.iso8601
end
end

0 comments on commit 22bc40a

Please sign in to comment.