chronic_duration 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/chronic_duration.gemspec +2 -2
- data/lib/chronic_duration.rb +26 -1
- data/spec/chronic_duration_spec.rb +12 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -38,10 +38,19 @@ Examples of parse-able strings:
|
|
38
38
|
* '6 mos 1 day'
|
39
39
|
* '47 yrs 6 mos and 4d'
|
40
40
|
* 'two hours and twenty minutes'
|
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
|
+
|
41
50
|
|
42
51
|
== Contributors
|
43
52
|
|
44
|
-
jduff, olauzon
|
53
|
+
brianjlandau, jduff, olauzon
|
45
54
|
|
46
55
|
== TODO
|
47
56
|
|
@@ -49,4 +58,4 @@ jduff, olauzon
|
|
49
58
|
* Context specific matching (E.g., for '4m30s', assume 'm' is minutes not months)
|
50
59
|
* Smartly parse vacation-like durations (E.g., '4 days and 3 nights')
|
51
60
|
* :chrono output option should probably change to something like 4 days 4:00:12 instead of 4:04:00:12
|
52
|
-
* Other locale support
|
61
|
+
* Other locale support
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
data/chronic_duration.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{chronic_duration}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.9.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["hpoydar"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-28}
|
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 = [
|
data/lib/chronic_duration.rb
CHANGED
@@ -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
|
-
|
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
|
@@ -192,6 +210,9 @@ private
|
|
192
210
|
'day' => 'days',
|
193
211
|
'dy' => 'days',
|
194
212
|
'd' => 'days',
|
213
|
+
'weeks' => 'weeks',
|
214
|
+
'week' => 'weeks',
|
215
|
+
'w' => 'weeks',
|
195
216
|
'months' => 'months',
|
196
217
|
'mos' => 'months',
|
197
218
|
'years' => 'years',
|
@@ -200,6 +221,10 @@ private
|
|
200
221
|
}
|
201
222
|
end
|
202
223
|
|
224
|
+
def join_words
|
225
|
+
['and', 'with', 'plus']
|
226
|
+
end
|
227
|
+
|
203
228
|
def white_list
|
204
229
|
self.mappings.map {|k, v| k}
|
205
230
|
end
|
@@ -16,13 +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
|
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
|
20
23
|
}
|
21
24
|
|
22
25
|
it "should return nil if the string can't be parsed" do
|
23
26
|
ChronicDuration.parse('gobblygoo').should be_nil
|
24
27
|
end
|
25
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
|
+
|
26
35
|
it "should return a float if seconds are in decimals" do
|
27
36
|
ChronicDuration.parse('12 mins 3.141 seconds').is_a?(Float).should be_true
|
28
37
|
end
|
@@ -31,6 +40,8 @@ describe ChronicDuration, '.parse' do
|
|
31
40
|
ChronicDuration.parse('12 mins 3 seconds').is_a?(Integer).should be_true
|
32
41
|
end
|
33
42
|
|
43
|
+
|
44
|
+
|
34
45
|
@exemplars.each do |k, v|
|
35
46
|
it "should properly parse a duration like #{k}" do
|
36
47
|
ChronicDuration.parse(k).should == v
|
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.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hpoydar
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-28 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|