brianjlandau-chronic_duration 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,6 +39,14 @@ Examples of parse-able strings:
39
39
  * '47 yrs 6 mos and 4d'
40
40
  * 'two hours and twenty minutes'
41
41
  * '3 weeks and 2 days'
42
+
43
+ ChronicDuration.raise_exceptions can be set to true to raise exceptions when the string can't be parsed.
44
+
45
+ >> ChronicDuration.raise_exceptions = true
46
+ => true
47
+ >> ChronicDuration.parse('4 elephants and 3 Astroids')
48
+ ChronicDuration::DurationParseError: An invalid word "elephants" was used in the string to be parsed.
49
+
42
50
 
43
51
  == Contributors
44
52
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.2
1
+ 0.8.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{brianjlandau-chronic_duration}
8
- s.version = "0.8.2"
8
+ s.version = "0.8.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hpoydar", "brianjlandau"]
12
- s.date = %q{2010-01-22}
12
+ s.date = %q{2010-01-23}
13
13
  s.description = %q{A simple Ruby natural language parser for elapsed time. (For example, 4 hours and 30 minutes, 6 minutes 4 seconds, 3 days, etc.) Returns all results in seconds. Will return an integer unless you get tricky and need a float. (4 minutes and 13.47 seconds, for example.) The reverse can also be performed via the output method.}
14
14
  s.email = %q{hpoydar@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -2,6 +2,19 @@ require 'numerizer'
2
2
  module ChronicDuration
3
3
  extend self
4
4
 
5
+ class DurationParseError < StandardError
6
+ end
7
+
8
+ @@raise_exceptions = false
9
+
10
+ def self.raise_exceptions
11
+ !!@@raise_exceptions
12
+ end
13
+
14
+ def self.raise_exceptions=(value)
15
+ @@raise_exceptions = !!value
16
+ end
17
+
5
18
  # Given a string representation of elapsed time,
6
19
  # return an integer (or float, if fractions of a
7
20
  # second are input)
@@ -166,7 +179,12 @@ private
166
179
  res << word.strip
167
180
  next
168
181
  end
169
- res << mappings[word.strip] if mappings.has_key?(word.strip)
182
+ stripped_word = word.strip.gsub(/^,/, '').gsub(/,$/, '')
183
+ if mappings.has_key?(stripped_word)
184
+ res << mappings[stripped_word]
185
+ elsif !join_words.include?(stripped_word) and ChronicDuration.raise_exceptions
186
+ raise DurationParseError, "An invalid word #{word.inspect} was used in the string to be parsed."
187
+ end
170
188
  end
171
189
  res.join(' ')
172
190
  end
@@ -203,6 +221,10 @@ private
203
221
  }
204
222
  end
205
223
 
224
+ def join_words
225
+ ['and', 'with', 'plus']
226
+ end
227
+
206
228
  def white_list
207
229
  self.mappings.map {|k, v| k}
208
230
  end
@@ -16,14 +16,22 @@ describe ChronicDuration, '.parse' do
16
16
  '47 yrs 6 mos and 4.5d' => 47 * 31557600 + 6 * 30 * 24 * 3600 + 4.5 * 24 * 3600,
17
17
  'two hours and twenty minutes' => 2 * 3600 + 20 * 60,
18
18
  'four hours and forty minutes' => 4 * 3600 + 40 * 60,
19
- 'four hours and fourty minutes' => 4 * 3600 + 40 * 60,
20
- '3 weeks and 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2
19
+ 'four hours, and fourty minutes' => 4 * 3600 + 40 * 60,
20
+ '3 weeks and, 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
21
+ '3 weeks, plus 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
22
+ '3 weeks with 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2
21
23
  }
22
24
 
23
25
  it "should return nil if the string can't be parsed" do
24
26
  ChronicDuration.parse('gobblygoo').should be_nil
25
27
  end
26
28
 
29
+ it "should raise an exception if the string can't be parsed and @@raise_exceptions is set to true" do
30
+ ChronicDuration.raise_exceptions = true
31
+ lambda { ChronicDuration.parse('23 gobblygoos') }.should raise_exception(ChronicDuration::DurationParseError)
32
+ ChronicDuration.raise_exceptions = false
33
+ end
34
+
27
35
  it "should return a float if seconds are in decimals" do
28
36
  ChronicDuration.parse('12 mins 3.141 seconds').is_a?(Float).should be_true
29
37
  end
@@ -32,6 +40,8 @@ describe ChronicDuration, '.parse' do
32
40
  ChronicDuration.parse('12 mins 3 seconds').is_a?(Integer).should be_true
33
41
  end
34
42
 
43
+
44
+
35
45
  @exemplars.each do |k, v|
36
46
  it "should properly parse a duration like #{k}" do
37
47
  ChronicDuration.parse(k).should == v
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brianjlandau-chronic_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - hpoydar
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-22 00:00:00 -05:00
13
+ date: 2010-01-23 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency