ruby-duration 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
+ - 2.0.0
4
5
  - jruby-19mode
5
- - ruby-head
6
6
  - jruby-head
@@ -1,6 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'i18n'
3
3
  require 'active_support/core_ext'
4
+ require 'iso8601'
4
5
 
5
6
  # Duration objects are simple mechanisms that allow you to operate on durations
6
7
  # of time. They allow you to know how much time has passed since a certain
@@ -39,6 +40,8 @@ class Duration
39
40
  unit = unit.to_sym
40
41
  @seconds += args[unit].to_i * multiple if args.key?(unit)
41
42
  end
43
+ elsif args.kind_of?(String) and args[0] == 'P'
44
+ @seconds = ISO8601::Duration.new(args).to_seconds
42
45
  else
43
46
  @seconds = args.to_i
44
47
  end
@@ -46,6 +49,14 @@ class Duration
46
49
  calculate!
47
50
  end
48
51
 
52
+ def self.load string
53
+ self.new(string)
54
+ end
55
+
56
+ def self.dump duration
57
+ duration.iso8601
58
+ end
59
+
49
60
  # Compare this duration to another (or objects that respond to #to_i)
50
61
  def <=>(other)
51
62
  return false unless other.is_a?(Duration)
@@ -81,8 +92,8 @@ class Duration
81
92
  def iso8601
82
93
  output = 'P'
83
94
 
84
- output << "#{weeks}W" if weeks > 0
85
- output << "#{days}D" if days > 0
95
+ number_of_days = weeks * 7 + days
96
+ output << "#{number_of_days}D" if number_of_days > 0
86
97
  if seconds > 0 || minutes > 0 || hours > 0
87
98
  output << 'T'
88
99
  output << "#{hours}H" if hours > 0
@@ -115,6 +126,10 @@ class Duration
115
126
  # %tm => total minutes
116
127
  # %ts => total seconds
117
128
  # %t => total seconds
129
+ # %MP => minutes with UTF-8 prime
130
+ # %SP => seconds with UTF-8 double-prime
131
+ # %MH => minutes with HTML prime
132
+ # %SH => seconds with HTML double-prime
118
133
  # %H => zero-padded hours
119
134
  # %M => zero-padded minutes
120
135
  # %S => zero-padded seconds
@@ -161,6 +176,10 @@ class Duration
161
176
  'tm' => Proc.new { total_minutes },
162
177
  'ts' => @total,
163
178
  't' => @total,
179
+ 'MP' => Proc.new { "#{@minutes}′"},
180
+ 'SP' => Proc.new { "#{@seconds}″"},
181
+ 'MH' => Proc.new { "#{@minutes}&#8242;"},
182
+ 'SH' => Proc.new { "#{@seconds}&#8243;"},
164
183
  'H' => @hours.to_s.rjust(2, '0'),
165
184
  'M' => @minutes.to_s.rjust(2, '0'),
166
185
  'S' => @seconds.to_s.rjust(2, '0'),
@@ -172,10 +191,10 @@ class Duration
172
191
  'tdu'=> Proc.new { "#{total_days} #{i18n_for(:total_day)}"},
173
192
  'thu'=> Proc.new { "#{total_hours} #{i18n_for(:total_hour)}"},
174
193
  'tmu'=> Proc.new { "#{total_minutes} #{i18n_for(:total_minute)}"},
175
- 'tsu'=> Proc.new { "#{total} #{i18n_for(:total)}"}
194
+ 'tsu'=> Proc.new { "#{total} #{i18n_for(:total)}"},
176
195
  }
177
196
 
178
- format_str.gsub(/%?%(w|d|h|m|s|t([dhms]u?)?|H|M|S|~(?:s|m|h|d|w))/) do |match|
197
+ format_str.gsub(/%?%(w|d|h|m|s|t([dhms]u?)?|MP|SP|MH|SH|H|M|S|~(?:s|m|h|d|w))/) do |match|
179
198
  match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
180
199
  end.gsub('%%', '%')
181
200
  end
@@ -1,3 +1,3 @@
1
1
  class Duration
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency "activesupport", ">= 3.0.0"
18
18
  s.add_dependency "i18n", ">= 0"
19
+ s.add_dependency "iso8601", ">= 0"
19
20
 
20
21
  s.add_development_dependency "bundler", ">= 1.0.0"
21
22
  s.add_development_dependency "minitest", ">= 0"
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'rubygems'
3
- require 'minitest/spec'
3
+ require 'minitest/autorun'
4
4
  begin
5
5
  require 'simplecov'
6
6
  SimpleCov.start do
@@ -12,4 +12,4 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
12
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
13
  require 'ruby-duration'
14
14
 
15
- MiniTest::Unit.autorun
15
+ MiniTest.autorun
@@ -28,6 +28,12 @@ describe "Duration" do
28
28
  assert_equal 9, d.total_days
29
29
  end
30
30
 
31
+ it 'should load and dump' do
32
+ duration = Duration.new(:seconds => 3)
33
+ assert_equal 'PT3S', Duration.dump(duration)
34
+ assert_equal Duration.load('PT3S'), duration
35
+ end
36
+
31
37
  describe "mathematical operations" do
32
38
 
33
39
  it "should +" do
@@ -123,6 +129,38 @@ describe "Duration" do
123
129
  end
124
130
  end
125
131
 
132
+ describe "output typographic marks for minutes and seconds" do
133
+ it "uses a UTF-8 double prime for seconds" do
134
+ d = Duration.new(:seconds => 14)
135
+ assert_equal "14″", d.format("%SP")
136
+ end
137
+
138
+ it "uses a UTF-8 prime for seconds" do
139
+ d = Duration.new(:minutes => 21)
140
+ assert_equal "21′", d.format("%MP")
141
+ end
142
+
143
+ it "uses a HTML double prime for seconds" do
144
+ d = Duration.new(:seconds => 14)
145
+ assert_equal "14&#8243;", d.format("%SH")
146
+ end
147
+
148
+ it "uses a HTML prime for seconds" do
149
+ d = Duration.new(:minutes => 21)
150
+ assert_equal "21&#8242;", d.format("%MH")
151
+ end
152
+ end
153
+
154
+ describe "creation from iso_8601" do
155
+ it "should work" do
156
+ assert_equal 60, Duration.new("PT1M").to_i
157
+ assert_equal 3630, Duration.new("PT1H30S").to_i
158
+
159
+ duration = Duration.new(:weeks => 1, :days => 1, :hours => 2, :minutes => 1, :seconds => 2)
160
+ assert_equal duration, Duration.new(duration.iso8601)
161
+ end
162
+ end
163
+
126
164
  describe "#iso_6801" do
127
165
  it "should format seconds" do
128
166
  d = Duration.new(:seconds => 1)
@@ -144,20 +182,20 @@ describe "Duration" do
144
182
  assert_equal "P1D", d.iso8601
145
183
  end
146
184
 
147
- it "should format weeks" do
185
+ it "should format weeks as ndays" do
148
186
  d = Duration.new(:weeks => 1)
149
- assert_equal "P1W", d.iso8601
187
+ assert_equal "P7D", d.iso8601
150
188
  end
151
189
 
152
190
  it "should format only with given values" do
153
191
  d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
154
- assert_equal "P1W2DT3H4M5S", d.iso8601
192
+ assert_equal "P9DT3H4M5S", d.iso8601
155
193
 
156
194
  d = Duration.new(:weeks => 1, :hours => 2, :seconds => 3)
157
- assert_equal "P1WT2H3S", d.iso8601
195
+ assert_equal "P7DT2H3S", d.iso8601
158
196
 
159
197
  d = Duration.new(:weeks => 1, :days => 2)
160
- assert_equal "P1W2D", d.iso8601
198
+ assert_equal "P9D", d.iso8601
161
199
 
162
200
  d = Duration.new(:hours => 1, :seconds => 30)
163
201
  assert_equal "PT1H30S", d.iso8601
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: 3.0.0
4
+ version: 3.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-22 00:00:00.000000000 Z
13
+ date: 2013-07-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -44,6 +44,22 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: iso8601
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: bundler
49
65
  requirement: !ruby/object:Gem::Requirement
@@ -180,7 +196,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
180
196
  version: '0'
181
197
  segments:
182
198
  - 0
183
- hash: 370806958351175358
199
+ hash: 1867546005898660473
184
200
  required_rubygems_version: !ruby/object:Gem::Requirement
185
201
  none: false
186
202
  requirements: