natural_time 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +10 -0
- data/README.rdoc +9 -2
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/lib/natural_time.rb +24 -5
- data/natural_time.gemspec +5 -5
- data/spec/natural_time_spec.rb +81 -0
- metadata +4 -4
- data/test.rb +0 -6
data/History.txt
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
== 0.0.3 2010-01-22
|
2
|
+
* major enhancements
|
3
|
+
* specify level of precision
|
4
|
+
* distance method shows whether it's past or future
|
5
|
+
* minor enhancements
|
6
|
+
* bug fixes
|
7
|
+
|
8
|
+
== 0.0.2 2010-01-13
|
9
|
+
* major enhancements
|
10
|
+
* outputs duration of time in natural language
|
data/README.rdoc
CHANGED
@@ -2,10 +2,17 @@
|
|
2
2
|
|
3
3
|
NaturalTime outputs a duration in natural language.
|
4
4
|
|
5
|
-
|
5
|
+
== Usage
|
6
6
|
|
7
|
-
|
7
|
+
NaturalTime.new(65).to_sentence #=> "1 minute, 5 seconds"
|
8
8
|
|
9
|
+
NaturalTime.new(120).to_sentence #=> "2 minutes"
|
10
|
+
|
11
|
+
NaturalTime instances can also be output to an array:
|
12
|
+
|
13
|
+
NaturalTime.new(65).to_array #=> ["1 minutes", "5 seconds"]
|
14
|
+
|
15
|
+
NaturalTime.new(120).to_array #=> ["2 minutes"]
|
9
16
|
|
10
17
|
== Note on Patches/Pull Requests
|
11
18
|
|
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/progressions/natural_time"
|
12
12
|
gem.authors = ["Jeff Coleman"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.6"
|
14
|
-
gem.
|
14
|
+
gem.add_runtime_dependency "activesupport", ">= 0"
|
15
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
16
|
end
|
17
17
|
Jeweler::GemcutterTasks.new
|
@@ -29,6 +29,7 @@ end
|
|
29
29
|
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
30
|
spec.libs << 'lib' << 'spec'
|
31
31
|
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov_opts = ['--exclude', '.gem,Library,spec', '--sort', 'coverage']
|
32
33
|
spec.rcov = true
|
33
34
|
end
|
34
35
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/natural_time.rb
CHANGED
@@ -2,13 +2,15 @@ require 'rubygems'
|
|
2
2
|
require 'active_support'
|
3
3
|
|
4
4
|
class NaturalTime
|
5
|
-
attr_accessor :duration
|
5
|
+
attr_accessor :duration, :precision, :past
|
6
6
|
|
7
7
|
UNITS_OF_TIME = [["year", "years"], ["month", "months"], ["week", "weeks"], ["day", "days"], ["hour", "hours"], ["minute", "minutes"]]
|
8
8
|
|
9
|
-
def initialize(duration_in_seconds)
|
9
|
+
def initialize(duration_in_seconds, options={})
|
10
|
+
@precision = options[:precision]
|
10
11
|
duration_in_seconds = duration_in_seconds.to_i
|
11
|
-
@
|
12
|
+
@past = duration_in_seconds < 1
|
13
|
+
@duration = duration_in_seconds.abs
|
12
14
|
elapsed_time = elapsed_time(duration_in_seconds)
|
13
15
|
end
|
14
16
|
|
@@ -24,10 +26,23 @@ class NaturalTime
|
|
24
26
|
natural_time
|
25
27
|
end
|
26
28
|
|
27
|
-
def
|
29
|
+
def to_a
|
28
30
|
elapsed_time(duration)
|
29
31
|
end
|
30
32
|
|
33
|
+
def to_array
|
34
|
+
to_a
|
35
|
+
end
|
36
|
+
|
37
|
+
def distance
|
38
|
+
if past
|
39
|
+
modifier = "ago"
|
40
|
+
else
|
41
|
+
modifier = "from now"
|
42
|
+
end
|
43
|
+
"#{to_sentence} #{modifier}"
|
44
|
+
end
|
45
|
+
|
31
46
|
def elapsed_time(duration_in_seconds)
|
32
47
|
elapsed_time = []
|
33
48
|
|
@@ -44,6 +59,10 @@ class NaturalTime
|
|
44
59
|
str = seconds == 1 ? "second" : "seconds"
|
45
60
|
elapsed_time << "#{seconds.to_i} #{str}" unless (seconds == 0 && elapsed_time.compact.length > 0)
|
46
61
|
|
47
|
-
|
62
|
+
if precision
|
63
|
+
elapsed_time.compact.first(precision)
|
64
|
+
else
|
65
|
+
elapsed_time.compact
|
66
|
+
end
|
48
67
|
end
|
49
68
|
end
|
data/natural_time.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{natural_time}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jeff Coleman"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-22}
|
13
13
|
s.description = %q{Smart enumeration of durations in natural language--"4 hours, 3 minutes and 2 seconds"}
|
14
14
|
s.email = %q{progressions@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"History.txt",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
24
25
|
"Rakefile",
|
@@ -27,8 +28,7 @@ Gem::Specification.new do |s|
|
|
27
28
|
"natural_time.gemspec",
|
28
29
|
"spec/natural_time_spec.rb",
|
29
30
|
"spec/spec.opts",
|
30
|
-
"spec/spec_helper.rb"
|
31
|
-
"test.rb"
|
31
|
+
"spec/spec_helper.rb"
|
32
32
|
]
|
33
33
|
s.homepage = %q{http://github.com/progressions/natural_time}
|
34
34
|
s.rdoc_options = ["--charset=UTF-8"]
|
@@ -46,7 +46,7 @@ Gem::Specification.new do |s|
|
|
46
46
|
|
47
47
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
48
|
s.add_development_dependency(%q<rspec>, [">= 1.2.6"])
|
49
|
-
s.
|
49
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
50
50
|
else
|
51
51
|
s.add_dependency(%q<rspec>, [">= 1.2.6"])
|
52
52
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
data/spec/natural_time_spec.rb
CHANGED
@@ -199,6 +199,53 @@ describe "Naturaltime" do
|
|
199
199
|
it "should return remainder in months and days and hours and minutes and seconds" do
|
200
200
|
NaturalTime.new(1.year + 1.month + 1.week + 1.day + 1.hour + 5.minutes + 5.seconds).natural_time.should == "1 year, 1 month, 1 week, 1 day, 1 hour, 5 minutes, 5 seconds"
|
201
201
|
end
|
202
|
+
|
203
|
+
describe "precision" do
|
204
|
+
it "should limit the precision to 1" do
|
205
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 1).natural_time.should == "1 week"
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should limit the precision to 2" do
|
209
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 2).natural_time.should == "1 week, 1 minute"
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should show the distance with precision of 1 in the past" do
|
213
|
+
distance = -1 * (1.week + 1.minutes + 1.second)
|
214
|
+
NaturalTime.new(distance, :precision => 1).distance.should == "1 week ago"
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should show the distance with precision of 2 in the past" do
|
218
|
+
distance = -1 * (1.week + 1.minutes + 1.second)
|
219
|
+
NaturalTime.new(distance, :precision => 2).distance.should == "1 week and 1 minute ago"
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should show the distance with precision of 1 in the future" do
|
223
|
+
distance = (1.week + 1.minutes + 1.second)
|
224
|
+
NaturalTime.new(distance, :precision => 1).distance.should == "1 week from now"
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should show the distance with precision of 2 in the future" do
|
228
|
+
distance = (1.week + 1.minutes + 1.second)
|
229
|
+
NaturalTime.new(distance, :precision => 2).distance.should == "1 week and 1 minute from now"
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
describe "distances" do
|
234
|
+
it "should give the distance in the past" do
|
235
|
+
NaturalTime.new(-1).distance.should == "1 second ago"
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should give the distance in the past" do
|
239
|
+
NaturalTime.new(-2).distance.should == "2 seconds ago"
|
240
|
+
end
|
241
|
+
it "should give the distance in the future" do
|
242
|
+
NaturalTime.new(1).distance.should == "1 second from now"
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should give the distance in the future" do
|
246
|
+
NaturalTime.new(2).distance.should == "2 seconds from now"
|
247
|
+
end
|
248
|
+
end
|
202
249
|
end
|
203
250
|
|
204
251
|
describe "to_s" do
|
@@ -208,17 +255,51 @@ describe "Naturaltime" do
|
|
208
255
|
@natural_time.to_s.should == @natural_time.natural_time
|
209
256
|
end
|
210
257
|
end
|
258
|
+
|
259
|
+
describe "precision" do
|
260
|
+
it "should limit the precision to 1" do
|
261
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 1).to_s.should == "1 week"
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should limit the precision to 2" do
|
265
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 2).to_s.should == "1 week, 1 minute"
|
266
|
+
end
|
267
|
+
end
|
211
268
|
end
|
212
269
|
|
213
270
|
describe "to_sentence" do
|
214
271
|
it "should put 'and' before the last entry in the list" do
|
215
272
|
NaturalTime.new(1.minute + 1.second).to_sentence.should == "1 minute and 1 second"
|
216
273
|
end
|
274
|
+
|
275
|
+
describe "precision" do
|
276
|
+
it "should limit the precision to 1" do
|
277
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 1).to_sentence.should == "1 week"
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should limit the precision to 2" do
|
281
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 2).to_sentence.should == "1 week and 1 minute"
|
282
|
+
end
|
283
|
+
end
|
217
284
|
end
|
218
285
|
|
219
286
|
describe "to_array" do
|
220
287
|
it "should return an array" do
|
221
288
|
NaturalTime.new(1.minutes + 1.second).to_array.should == ["1 minute", "1 second"]
|
222
289
|
end
|
290
|
+
|
291
|
+
it "should return an array" do
|
292
|
+
NaturalTime.new(1.minutes + 1.second).to_a.should == ["1 minute", "1 second"]
|
293
|
+
end
|
294
|
+
|
295
|
+
describe "precision" do
|
296
|
+
it "should limit the precision to 1" do
|
297
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 1).to_a.should == ["1 week"]
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should limit the precision to 2" do
|
301
|
+
NaturalTime.new(1.week + 1.minutes + 1.second, :precision => 2).to_a.should == ["1 week", "1 minute"]
|
302
|
+
end
|
303
|
+
end
|
223
304
|
end
|
224
305
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: natural_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Coleman
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-22 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
type: :
|
27
|
+
type: :runtime
|
28
28
|
version_requirement:
|
29
29
|
version_requirements: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
@@ -44,6 +44,7 @@ extra_rdoc_files:
|
|
44
44
|
files:
|
45
45
|
- .document
|
46
46
|
- .gitignore
|
47
|
+
- History.txt
|
47
48
|
- LICENSE
|
48
49
|
- README.rdoc
|
49
50
|
- Rakefile
|
@@ -53,7 +54,6 @@ files:
|
|
53
54
|
- spec/natural_time_spec.rb
|
54
55
|
- spec/spec.opts
|
55
56
|
- spec/spec_helper.rb
|
56
|
-
- test.rb
|
57
57
|
has_rdoc: true
|
58
58
|
homepage: http://github.com/progressions/natural_time
|
59
59
|
licenses: []
|