chronic_duration 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,8 +12,16 @@ The reverse can also be accomplished with the output method. So pass in seconds
12
12
  => true
13
13
  >> ChronicDuration.parse('4 minutes and 30 seconds')
14
14
  => 270
15
+ >> ChronicDuration.parse('0 seconds')
16
+ => nil
17
+ >> ChronicDuration.parse('0 seconds', :keep_zero => true)
18
+ => 0
15
19
  >> ChronicDuration.output(270)
16
20
  => 4 mins 30 secs
21
+ >> ChronicDuration.output(0)
22
+ => nil
23
+ >> ChronicDuration.output(0, :keep_zero => true)
24
+ => 0 secs
17
25
  >> ChronicDuration.output(270, :format => :short)
18
26
  => 4m 30s
19
27
  >> ChronicDuration.output(270, :format => :long)
@@ -57,7 +65,7 @@ Also looking for additional maintainers.
57
65
 
58
66
  ## Contributors
59
67
 
60
- brianjlandau, jduff, olauzon, roboman, ianlevesque
68
+ pdf, brianjlandau, jduff, olauzon, roboman, ianlevesque
61
69
 
62
70
  ## TODO
63
71
 
@@ -22,7 +22,7 @@ module ChronicDuration
22
22
  # second are input)
23
23
  def parse(string, opts = {})
24
24
  result = calculate_from_words(cleanup(string), opts)
25
- result == 0 ? nil : result
25
+ (!opts[:keep_zero] and result == 0) ? nil : result
26
26
  end
27
27
 
28
28
  # Given an integer and an optional format,
@@ -32,6 +32,7 @@ module ChronicDuration
32
32
  seconds = int if seconds - int == 0 # if seconds end with .0
33
33
 
34
34
  opts[:format] ||= :default
35
+ opts[:keep_zero] ||= false
35
36
 
36
37
  years = months = weeks = days = hours = minutes = 0
37
38
 
@@ -110,7 +111,9 @@ module ChronicDuration
110
111
  next if t == :weeks && !opts[:weeks]
111
112
  num = eval(t.to_s)
112
113
  num = ("%.#{decimal_places}f" % num) if num.is_a?(Float) && t == :seconds
113
- humanize_time_unit( num, dividers[t], dividers[:pluralize], dividers[:keep_zero] )
114
+ keep_zero = dividers[:keep_zero]
115
+ keep_zero ||= opts[:keep_zero] if t == :seconds
116
+ humanize_time_unit( num, dividers[t], dividers[:pluralize], keep_zero )
114
117
  end.compact!
115
118
 
116
119
  result = result[0...opts[:units]] if opts[:units]
@@ -1,3 +1,3 @@
1
1
  module ChronicDuration
2
- VERSION = '0.10.1'
2
+ VERSION = '0.10.2'
3
3
  end
@@ -31,6 +31,14 @@ describe ChronicDuration, '.parse' do
31
31
  ChronicDuration.parse('gobblygoo').should be_nil
32
32
  end
33
33
 
34
+ it "should return nil if the string parses as zero" do
35
+ ChronicDuration.parse('0').should be_nil
36
+ end
37
+
38
+ it "should return zero if the string parses as zero and the keep_zero option is true" do
39
+ ChronicDuration.parse('0', :keep_zero => true).should == 0
40
+ end
41
+
34
42
  it "should raise an exception if the string can't be parsed and @@raise_exceptions is set to true" do
35
43
  ChronicDuration.raise_exceptions = true
36
44
  lambda { ChronicDuration.parse('23 gobblygoos') }.should raise_exception(ChronicDuration::DurationParseError)
@@ -202,6 +210,33 @@ describe ChronicDuration, '.output' do
202
210
  end
203
211
  end
204
212
 
213
+ @keep_zero_exemplars = {
214
+ (true) =>
215
+ {
216
+ :micro => '0s',
217
+ :short => '0s',
218
+ :default => '0 secs',
219
+ :long => '0 seconds',
220
+ :chrono => '0'
221
+ },
222
+ (false) =>
223
+ {
224
+ :micro => nil,
225
+ :short => nil,
226
+ :default => nil,
227
+ :long => nil,
228
+ :chrono => '0'
229
+ },
230
+ }
231
+
232
+ @keep_zero_exemplars.each do |k, v|
233
+ v.each do |key, val|
234
+ it "should properly output a duration of 0 seconds as #{val.nil? ? "nil" : val} using the #{key.to_s} format option, if the keep_zero option is #{k.to_s}" do
235
+ ChronicDuration.output(0, :format => key, :keep_zero => k).should == val
236
+ end
237
+ end
238
+ end
239
+
205
240
  it "should show weeks when needed" do
206
241
  ChronicDuration.output(15*24*60*60, :weeks => true).should =~ /.*wk.*/
207
242
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronic_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.10.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-23 00:00:00.000000000 Z
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: numerizer