natural_time 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jeff Coleman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = natural_time
2
+
3
+ NaturalTime outputs a duration in natural language.
4
+
5
+ 65 seconds becomes "1 minute, 5 seconds".
6
+
7
+ 120 seconds becomes "2 minutes".
8
+
9
+
10
+ == Note on Patches/Pull Requests
11
+
12
+ * Fork the project.
13
+ * Make your feature addition or bug fix.
14
+ * Add tests for it. This is important so I don't break it in a
15
+ future version unintentionally.
16
+ * Commit, do not mess with rakefile, version, or history.
17
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2010 Jeff Coleman. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "natural_time"
8
+ gem.summary = %Q{Smart enumeration of durations in natural language}
9
+ gem.description = %Q{Smart enumeration of durations in natural language--"4 hours, 3 minutes and 2 seconds"}
10
+ gem.email = "progressions@gmail.com"
11
+ gem.homepage = "http://github.com/progressions/natural_time"
12
+ gem.authors = ["Jeff Coleman"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.6"
14
+ gem.add_development_dependency "activesupport", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ spec.spec_opts = ['-c']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "naturaltime #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ class NaturalTime
5
+ attr_accessor :duration
6
+
7
+ UNITS_OF_TIME = [["year", "years"], ["month", "months"], ["week", "weeks"], ["day", "days"], ["hour", "hours"], ["minute", "minutes"]]
8
+
9
+ def initialize(duration_in_seconds)
10
+ duration_in_seconds = duration_in_seconds.to_i
11
+ @duration = duration_in_seconds
12
+ elapsed_time = elapsed_time(duration_in_seconds)
13
+ end
14
+
15
+ def to_sentence
16
+ to_array.to_sentence
17
+ end
18
+
19
+ def natural_time
20
+ to_array.join(", ")
21
+ end
22
+
23
+ def to_s
24
+ natural_time
25
+ end
26
+
27
+ def to_array
28
+ elapsed_time(duration)
29
+ end
30
+
31
+ def elapsed_time(duration_in_seconds)
32
+ elapsed_time = []
33
+
34
+ seconds_left = duration_in_seconds
35
+
36
+ UNITS_OF_TIME.each do |period|
37
+ amount = (seconds_left / 1.send(period.first)).to_i
38
+ str = amount == 1 ? period[0] : period[1]
39
+ elapsed_time << "#{amount} #{str}" if amount > 0
40
+ seconds_left = seconds_left % 1.send(period.first)
41
+ end
42
+
43
+ seconds = seconds_left % 1.minute
44
+ str = seconds == 1 ? "second" : "seconds"
45
+ elapsed_time << "#{seconds.to_i} #{str}" unless (seconds == 0 && elapsed_time.compact.length > 0)
46
+
47
+ elapsed_time.compact
48
+ end
49
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{natural_time}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jeff Coleman"]
12
+ s.date = %q{2010-01-13}
13
+ s.description = %q{Smart enumeration of durations in natural language--"4 hours, 3 minutes and 2 seconds"}
14
+ s.email = %q{progressions@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/natural_time.rb",
27
+ "natural_time.gemspec",
28
+ "spec/natural_time_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb",
31
+ "test.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/progressions/natural_time}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Smart enumeration of durations in natural language}
38
+ s.test_files = [
39
+ "spec/natural_time_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<rspec>, [">= 1.2.6"])
49
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 1.2.6"])
52
+ s.add_dependency(%q<activesupport>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.6"])
56
+ s.add_dependency(%q<activesupport>, [">= 0"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,224 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Naturaltime" do
4
+ describe "seconds" do
5
+ it "should return zero seconds" do
6
+ NaturalTime.new(0).natural_time.should == "0 seconds"
7
+ end
8
+
9
+ it "should return singular" do
10
+ NaturalTime.new(1).natural_time.should == "1 second"
11
+ end
12
+
13
+ it "should return seconds" do
14
+ (2..59).each do |i|
15
+ NaturalTime.new(i).natural_time.should == "#{i} seconds"
16
+ end
17
+ end
18
+
19
+ it "should not return 60 seconds" do
20
+ NaturalTime.new(60.seconds).natural_time.should_not match(/60 seconds/)
21
+ end
22
+
23
+ it "should return remainder in seconds" do
24
+ NaturalTime.new(65.seconds).natural_time.should match(/ 5 seconds/)
25
+ end
26
+ end
27
+
28
+ describe "minutes" do
29
+ it "should return singular" do
30
+ NaturalTime.new(60.seconds).natural_time.should == "1 minute"
31
+ end
32
+
33
+ it "should return whole minutes" do
34
+ (2..59).each do |i|
35
+ NaturalTime.new(i.minutes).natural_time.should == "#{i} minutes"
36
+ end
37
+ end
38
+
39
+ it "should not return 60 minutes" do
40
+ NaturalTime.new(60.minutes).natural_time.should_not match(/60 minutes/)
41
+ end
42
+
43
+ it "should return remainder in seconds" do
44
+ NaturalTime.new(65.seconds).natural_time.should == "1 minute, 5 seconds"
45
+ end
46
+ end
47
+
48
+ describe "hours" do
49
+ it "should return singular" do
50
+ NaturalTime.new(1.hour).natural_time.should == "1 hour"
51
+ end
52
+
53
+ it "should return whole hours" do
54
+ (2..23).each do |i|
55
+ NaturalTime.new(i.hours).natural_time.should == "#{i} hours"
56
+ end
57
+ end
58
+
59
+ it "should not return 24 hours" do
60
+ NaturalTime.new(24.hours).natural_time.should_not match(/24 hours/)
61
+ end
62
+
63
+ it "should return remainder in minutes" do
64
+ NaturalTime.new(1.hour + 5.minutes).natural_time.should == "1 hour, 5 minutes"
65
+ end
66
+
67
+ it "should return remainder in minutes and seconds" do
68
+ NaturalTime.new(1.hour + 5.minutes + 5.seconds).natural_time.should == "1 hour, 5 minutes, 5 seconds"
69
+ end
70
+ end
71
+
72
+ describe "days" do
73
+ it "should return singular" do
74
+ NaturalTime.new(1.day).natural_time.should == "1 day"
75
+ end
76
+
77
+ it "should return whole days" do
78
+ (2..6).each do |i|
79
+ NaturalTime.new(i.days).natural_time.should == "#{i} days"
80
+ end
81
+ end
82
+
83
+ it "should not return 7 days" do
84
+ NaturalTime.new(7.days).natural_time.should_not match(/7 days/)
85
+ end
86
+
87
+ it "should return remainder in hours" do
88
+ NaturalTime.new(1.day + 1.hour).natural_time.should == "1 day, 1 hour"
89
+ end
90
+
91
+ it "should return remainder in hours and minutes" do
92
+ NaturalTime.new(1.day + 1.hour + 5.minutes).natural_time.should == "1 day, 1 hour, 5 minutes"
93
+ end
94
+
95
+ it "should return remainder in hours and minutes and seconds" do
96
+ NaturalTime.new(1.day + 1.hour + 5.minutes + 5.seconds).natural_time.should == "1 day, 1 hour, 5 minutes, 5 seconds"
97
+ end
98
+ end
99
+
100
+ describe "weeks" do
101
+ it "should return singular" do
102
+ NaturalTime.new(1.week).natural_time.should == "1 week"
103
+ end
104
+
105
+ it "should return whole weeks" do
106
+ (2..4).each do |i|
107
+ NaturalTime.new(i.weeks).natural_time.should == "#{i} weeks"
108
+ end
109
+ end
110
+
111
+ it "should not return 5 weeks" do
112
+ NaturalTime.new(5.weeks).natural_time.should_not match(/5 weeks/)
113
+ end
114
+
115
+ it "should return remainder in days" do
116
+ NaturalTime.new(1.week + 1.day).natural_time.should == "1 week, 1 day"
117
+ end
118
+
119
+ it "should return remainder in days and hours" do
120
+ NaturalTime.new(1.week + 1.day + 1.hour).natural_time.should == "1 week, 1 day, 1 hour"
121
+ end
122
+
123
+ it "should return remainder in days and hours and minutes" do
124
+ NaturalTime.new(1.week + 1.day + 1.hour + 5.minutes).natural_time.should == "1 week, 1 day, 1 hour, 5 minutes"
125
+ end
126
+
127
+ it "should return remainder in days and hours and minutes and seconds" do
128
+ NaturalTime.new(1.week + 1.day + 1.hour + 5.minutes + 5.seconds).natural_time.should == "1 week, 1 day, 1 hour, 5 minutes, 5 seconds"
129
+ end
130
+ end
131
+
132
+ describe "months" do
133
+ it "should return singular" do
134
+ NaturalTime.new(1.month).natural_time.should == "1 month"
135
+ end
136
+
137
+ it "should return whole months" do
138
+ (2..4).each do |i|
139
+ NaturalTime.new(i.months).natural_time.should == "#{i} months"
140
+ end
141
+ end
142
+
143
+ it "should not return 12 months" do
144
+ NaturalTime.new(13.months).natural_time.should_not match(/13 months/)
145
+ end
146
+
147
+ it "should return remainder in weeks" do
148
+ NaturalTime.new(1.month + 1.week).natural_time.should == "1 month, 1 week"
149
+ end
150
+
151
+ it "should return remainder in days" do
152
+ NaturalTime.new(1.month + 1.week + 1.day).natural_time.should == "1 month, 1 week, 1 day"
153
+ end
154
+
155
+ it "should return remainder in days and hours" do
156
+ NaturalTime.new(1.month + 1.week + 1.day + 1.hour).natural_time.should == "1 month, 1 week, 1 day, 1 hour"
157
+ end
158
+
159
+ it "should return remainder in days and hours and minutes" do
160
+ NaturalTime.new(1.month + 1.week + 1.day + 1.hour + 5.minutes).natural_time.should == "1 month, 1 week, 1 day, 1 hour, 5 minutes"
161
+ end
162
+
163
+ it "should return remainder in days and hours and minutes and seconds" do
164
+ NaturalTime.new(1.month + 1.week + 1.day + 1.hour + 5.minutes + 5.seconds).natural_time.should == "1 month, 1 week, 1 day, 1 hour, 5 minutes, 5 seconds"
165
+ end
166
+ end
167
+
168
+ describe "years" do
169
+ it "should return singular" do
170
+ NaturalTime.new(1.year).natural_time.should == "1 year"
171
+ end
172
+
173
+ it "should return whole years" do
174
+ (2..4).each do |i|
175
+ NaturalTime.new(i.years).natural_time.should == "#{i} years"
176
+ end
177
+ end
178
+
179
+ it "should return remainder in months" do
180
+ NaturalTime.new(1.year + 1.month).natural_time.should == "1 year, 1 month"
181
+ end
182
+
183
+ it "should return remainder in months and weeks" do
184
+ NaturalTime.new(1.year + 1.month + 1.week).natural_time.should == "1 year, 1 month, 1 week"
185
+ end
186
+
187
+ it "should return remainder in months and days" do
188
+ NaturalTime.new(1.year + 1.month + 1.week + 1.day).natural_time.should == "1 year, 1 month, 1 week, 1 day"
189
+ end
190
+
191
+ it "should return remainder in months and days and hours" do
192
+ NaturalTime.new(1.year + 1.month + 1.week + 1.day + 1.hour).natural_time.should == "1 year, 1 month, 1 week, 1 day, 1 hour"
193
+ end
194
+
195
+ it "should return remainder in months and days and hours and minutes" do
196
+ NaturalTime.new(1.year + 1.month + 1.week + 1.day + 1.hour + 5.minutes).natural_time.should == "1 year, 1 month, 1 week, 1 day, 1 hour, 5 minutes"
197
+ end
198
+
199
+ it "should return remainder in months and days and hours and minutes and seconds" do
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
+ end
202
+ end
203
+
204
+ describe "to_s" do
205
+ it "should equal natural_time" do
206
+ (1..100).each do |i|
207
+ @natural_time = NaturalTime.new(i)
208
+ @natural_time.to_s.should == @natural_time.natural_time
209
+ end
210
+ end
211
+ end
212
+
213
+ describe "to_sentence" do
214
+ it "should put 'and' before the last entry in the list" do
215
+ NaturalTime.new(1.minute + 1.second).to_sentence.should == "1 minute and 1 second"
216
+ end
217
+ end
218
+
219
+ describe "to_array" do
220
+ it "should return an array" do
221
+ NaturalTime.new(1.minutes + 1.second).to_array.should == ["1 minute", "1 second"]
222
+ end
223
+ end
224
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'natural_time'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
data/test.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'lib/natural_time'
2
+
3
+ puts "Enter number of seconds: "
4
+ s = $stdin.gets
5
+
6
+ puts NaturalTime.new(s.to_i)
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: natural_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Coleman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-13 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.6
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Smart enumeration of durations in natural language--"4 hours, 3 minutes and 2 seconds"
36
+ email: progressions@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/natural_time.rb
52
+ - natural_time.gemspec
53
+ - spec/natural_time_spec.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - test.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/progressions/natural_time
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Smart enumeration of durations in natural language
85
+ test_files:
86
+ - spec/natural_time_spec.rb
87
+ - spec/spec_helper.rb