dotiw 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +117 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/dotiw.gemspec +55 -0
- data/lib/dotiw.rb +96 -0
- data/lib/i18n.rb +5 -0
- data/rails/init.rb +1 -0
- data/spec/dotiw_spec.rb +107 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/translations/en.yml +9 -0
- data/spec/translations/es.yml +9 -0
- metadata +77 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ryan Bigg
|
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.markdown
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# dotiw
|
2
|
+
|
3
|
+
dotiw is a plugin for Rails that overrides the default `distance_of_time_in_words` and provides a more accurate output. Do you crave accuracy down to the second? So do I. That's why I made this plugin. Take this for a totally kickass example:
|
4
|
+
|
5
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, true)
|
6
|
+
=> "1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds"
|
7
|
+
|
8
|
+
Also if one of the measurement is zero it will not output it:
|
9
|
+
|
10
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 2.months + 4.hours + 5.minutes + 6.seconds, true)
|
11
|
+
=> "1 year, 2 months, 4 hours, 5 minutes, and 6 seconds"
|
12
|
+
|
13
|
+
Better than "about 1 year", am I right? Of course I am.
|
14
|
+
|
15
|
+
This takes the same options plus an additional one on the end for passing options to the output (which uses `to_sentence`).
|
16
|
+
|
17
|
+
Oh, and did I mention it supports I18n? Oh yeah.
|
18
|
+
|
19
|
+
## distance\_of\_time\_in\_words\_hash
|
20
|
+
|
21
|
+
Don't like any format you're given? That's cool too! Here, have an indifferent hash version:
|
22
|
+
|
23
|
+
>> distance_of_time_in_words_hash(Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds)
|
24
|
+
=> {"days"=>3, "seconds"=>6, "minutes"=>5, "years"=>1, "hours"=>4, "months"=>2}
|
25
|
+
|
26
|
+
Indiferrent means that you can access all keys by their `String` or `Symbol` version.
|
27
|
+
|
28
|
+
### Options
|
29
|
+
|
30
|
+
#### :locale
|
31
|
+
|
32
|
+
The keys can be in your local language too:
|
33
|
+
|
34
|
+
>> distance_of_time_in_words_hash(Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, :locale => "es")
|
35
|
+
=> {"horas"=>4, "días"=>3, "minutos"=>5, "segundos"=>6, "años"=>1, "meses"=>2}
|
36
|
+
|
37
|
+
You are not guaranteed the order of the hash in Ruby 1.8.
|
38
|
+
|
39
|
+
## distance\_of\_time\_in\_words
|
40
|
+
|
41
|
+
### Options
|
42
|
+
|
43
|
+
The third argument for this method is whether or not to include seconds. By default this is `false` (because in Rails' `distance_of_time_in_words` it is), you can turn it on though by passing `true` as the third argument:
|
44
|
+
|
45
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 1.second, true)
|
46
|
+
=> "1 year, and 1 second"
|
47
|
+
|
48
|
+
Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility".
|
49
|
+
|
50
|
+
#### :locale
|
51
|
+
|
52
|
+
You can pass in a locale and it'll output it in whatever language you want (provided you have translations, otherwise it'll default to English):
|
53
|
+
|
54
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.minute, false, :locale => "es")
|
55
|
+
=> "1 minuto"
|
56
|
+
|
57
|
+
This will also be passed to `to_sentence`
|
58
|
+
|
59
|
+
#### :only
|
60
|
+
|
61
|
+
**Note that values passed into this option must be passed in as strings!**
|
62
|
+
|
63
|
+
Only want a specific measurement of time? No problem!
|
64
|
+
|
65
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute, false, :only => "minutes")
|
66
|
+
=> "1 minute"
|
67
|
+
|
68
|
+
You only want some? No problem too!
|
69
|
+
|
70
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.day + 1.minute, false, :only => ["minutes", "hours"])
|
71
|
+
=> "1 hour and 1 minute"
|
72
|
+
|
73
|
+
#### :except
|
74
|
+
|
75
|
+
**Note that values passed into this option must be passed in as strings!**
|
76
|
+
|
77
|
+
Don't want a measurement of time? No problem!
|
78
|
+
|
79
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute, false, :except => "minutes")
|
80
|
+
=> "1 hour"
|
81
|
+
|
82
|
+
Culling a whole group of measurements of time:
|
83
|
+
|
84
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.day + 1.minute, false, :except => ["minutes", "hours"])
|
85
|
+
=> "1 day"
|
86
|
+
|
87
|
+
#### :words_connector
|
88
|
+
|
89
|
+
**This is an option for `to_sentence`, defaults to ', '**
|
90
|
+
|
91
|
+
Using something other than a comma:
|
92
|
+
|
93
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, { :words_connector => ' - ' })
|
94
|
+
=> "1 hour - 1 minute, and 1 second"
|
95
|
+
|
96
|
+
#### :two\_words\_connector
|
97
|
+
|
98
|
+
**This is an option for `to_sentence`, defaults to ' and '**
|
99
|
+
|
100
|
+
Using something other than 'and':
|
101
|
+
|
102
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute, true, { :two_words_connector => ' plus ' })
|
103
|
+
=> "1 hour plus 1 minute"
|
104
|
+
|
105
|
+
#### :last\_word\_connector
|
106
|
+
|
107
|
+
**This is an option for `to_sentence`, defaults to ', and '**
|
108
|
+
|
109
|
+
Using something other than ', and':
|
110
|
+
|
111
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, { :last_word_connector => ', finally ' })
|
112
|
+
=> "1 hour, 1 minute, finally 1 second"
|
113
|
+
|
114
|
+
## Contributors
|
115
|
+
|
116
|
+
chendo - for talking through it with me and drawing on the whiteboard
|
117
|
+
Derander - correct Spanish translations
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "dotiw"
|
8
|
+
gem.summary = %Q{Better distance_of_time_in_words for Rails}
|
9
|
+
gem.description = %Q{Better distance_of_time_in_words for Rails}
|
10
|
+
gem.email = "radarlistener@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/radar/dotiw"
|
12
|
+
gem.authors = ["Ryan Bigg"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
if File.exist?('VERSION')
|
40
|
+
version = File.read('VERSION')
|
41
|
+
else
|
42
|
+
version = ""
|
43
|
+
end
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "dotiw #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
50
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.4
|
data/dotiw.gemspec
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{dotiw}
|
8
|
+
s.version = "0.2.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ryan Bigg"]
|
12
|
+
s.date = %q{2009-10-18}
|
13
|
+
s.description = %q{Better distance_of_time_in_words for Rails}
|
14
|
+
s.email = %q{radarlistener@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.markdown",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"dotiw.gemspec",
|
25
|
+
"lib/dotiw.rb",
|
26
|
+
"lib/i18n.rb",
|
27
|
+
"rails/init.rb",
|
28
|
+
"spec/dotiw_spec.rb",
|
29
|
+
"spec/spec_helper.rb",
|
30
|
+
"spec/translations/en.yml",
|
31
|
+
"spec/translations/es.yml"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/radar/dotiw}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Better distance_of_time_in_words for Rails}
|
38
|
+
s.test_files = [
|
39
|
+
"spec/dotiw_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>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
54
|
+
end
|
55
|
+
end
|
data/lib/dotiw.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
module DateHelper
|
4
|
+
def distance_of_time_in_words_hash(from_time, to_time, options={})
|
5
|
+
output = HashWithIndifferentAccess.new
|
6
|
+
from_time = from_time.to_time if from_time.respond_to?(:to_time)
|
7
|
+
to_time = to_time.to_time if to_time.respond_to?(:to_time)
|
8
|
+
|
9
|
+
distance = (from_time - to_time).abs
|
10
|
+
I18n.with_options :locale => options[:locale] do |locale|
|
11
|
+
while distance > 0
|
12
|
+
if distance < 1.minute
|
13
|
+
output[locale.t(:seconds, :default => "seconds")] = distance.to_i
|
14
|
+
distance = 0
|
15
|
+
elsif distance < 1.hour
|
16
|
+
output[locale.t(:minutes, :default => "minutes")], distance = distance.divmod(1.minute)
|
17
|
+
elsif distance < 1.day
|
18
|
+
output[locale.t(:hours, :default => "hours")], distance = distance.divmod(1.hour)
|
19
|
+
elsif distance < 28.days
|
20
|
+
output[locale.t(:days, :default => "days")], distance = distance.divmod(1.day)
|
21
|
+
# Time has to be greater than a month
|
22
|
+
else
|
23
|
+
smallest, largest = from_time < to_time ? [from_time, to_time] : [to_time, from_time]
|
24
|
+
|
25
|
+
months = (largest.year - smallest.year) * 12 + (largest.month - smallest.month)
|
26
|
+
years, months = months.divmod(12)
|
27
|
+
|
28
|
+
days = largest.day - smallest.day
|
29
|
+
|
30
|
+
# Will otherwise incorrectly say one more day if our range goes over a day.
|
31
|
+
days -= 1 if largest.hour < smallest.hour
|
32
|
+
|
33
|
+
if days < 0
|
34
|
+
# Convert the last month to days and add to total
|
35
|
+
months -= 1
|
36
|
+
last_month = largest.advance(:months => -1)
|
37
|
+
days += Time.days_in_month(last_month.month, last_month.year)
|
38
|
+
end
|
39
|
+
|
40
|
+
if months < 0
|
41
|
+
# Convert a year to months
|
42
|
+
years -= 1
|
43
|
+
months += 12
|
44
|
+
end
|
45
|
+
|
46
|
+
output[locale.t(:years, :default => "years")] = years
|
47
|
+
output[locale.t(:months, :default => "months")] = months
|
48
|
+
output[locale.t(:days, :default => "days")] = days
|
49
|
+
|
50
|
+
total_days, distance = distance.divmod(1.day)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
output
|
55
|
+
end
|
56
|
+
|
57
|
+
def distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {})
|
58
|
+
hash = distance_of_time_in_words_hash(from_time, to_time, options)
|
59
|
+
hash.delete(:seconds) if !include_seconds
|
60
|
+
I18n.with_options :locale => options[:locale] do |locale|
|
61
|
+
# Remove all the values that are nil.
|
62
|
+
time_measurements = [locale.t(:years, :default => "years"),
|
63
|
+
locale.t(:months, :default => "months"),
|
64
|
+
locale.t(:weeks, :default => "weeks"),
|
65
|
+
locale.t(:days, :default => "days"),
|
66
|
+
locale.t(:hours, :default => "hours"),
|
67
|
+
locale.t(:minutes, :default => "minutes"),
|
68
|
+
locale.t(:seconds, :default => "seconds")].delete_if do |key|
|
69
|
+
hash[key].nil? || hash[key].zero? ||
|
70
|
+
# Remove the keys that we don't want.
|
71
|
+
(!options[:except].nil? && options[:except].include?(key)) ||
|
72
|
+
# keep the keys we only want.
|
73
|
+
(options[:only] && !options[:only].include?(key))
|
74
|
+
end
|
75
|
+
|
76
|
+
options.delete(:except)
|
77
|
+
options.delete(:only)
|
78
|
+
output = []
|
79
|
+
time_measurements.each do |key|
|
80
|
+
name = hash[key] > 1 ? key : key.singularize
|
81
|
+
output += ["#{hash[key]} #{name}"]
|
82
|
+
end
|
83
|
+
|
84
|
+
# maybe only grab the first few values
|
85
|
+
if options[:precision]
|
86
|
+
output = output[0...options[:precision]]
|
87
|
+
options.delete(:precision)
|
88
|
+
end
|
89
|
+
|
90
|
+
output.to_sentence(options)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
data/lib/i18n.rb
ADDED
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'dotiw'
|
data/spec/dotiw_spec.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require 'dotiw'
|
3
|
+
|
4
|
+
describe "A better distance_of_time_in_words" do
|
5
|
+
include ActionView::Helpers::DateHelper
|
6
|
+
include ActionView::Helpers::TextHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
I18n.locale = :en
|
10
|
+
time = "01-08-2009".to_time
|
11
|
+
Time.stub!(:now).and_return(time)
|
12
|
+
Time.zone.stub!(:now).and_return(time)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "hash version" do
|
16
|
+
describe "giving correct numbers of" do
|
17
|
+
|
18
|
+
[:years, :months, :days, :minutes, :seconds].each do |name|
|
19
|
+
describe name do
|
20
|
+
it "exactly" do
|
21
|
+
hash = distance_of_time_in_words_hash(Time.now, Time.now + 1.send(name))
|
22
|
+
hash[name].should eql(1)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "two" do
|
26
|
+
hash = distance_of_time_in_words_hash(Time.now, Time.now + 2.send(name))
|
27
|
+
hash[name].should eql(2)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be happy with lots of measurements" do
|
33
|
+
hash = distance_of_time_in_words_hash(Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds)
|
34
|
+
hash[:years].should eql(1)
|
35
|
+
hash[:months].should eql(2)
|
36
|
+
hash[:days].should eql(3)
|
37
|
+
hash[:hours].should eql(4)
|
38
|
+
hash[:minutes].should eql(5)
|
39
|
+
hash[:seconds].should eql(6)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "debe estar contento con las mediciones en español" do
|
43
|
+
hash = distance_of_time_in_words_hash(Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, :locale => "es")
|
44
|
+
hash[:años].should eql(1)
|
45
|
+
hash[:meses].should eql(2)
|
46
|
+
hash[:días].should eql(3)
|
47
|
+
hash[:horas].should eql(4)
|
48
|
+
hash[:minutos].should eql(5)
|
49
|
+
hash[:segundos].should eql(6)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
it "debe hablar español" do
|
54
|
+
I18n.locale = :es
|
55
|
+
hash = distance_of_time_in_words_hash(Time.now, Time.now + 5.days)
|
56
|
+
hash["días"].should eql(5)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "real version" do
|
62
|
+
it "debe hablar español" do
|
63
|
+
distance_of_time_in_words(Time.now, Time.now + 5.days, true, :locale => "es").should eql("5 días")
|
64
|
+
end
|
65
|
+
|
66
|
+
[
|
67
|
+
[Time.now, Time.now + 5.days + 3.minutes, "5 days and 3 minutes"],
|
68
|
+
[Time.now, Time.now + 1.minute, "1 minute"],
|
69
|
+
[Time.now, Time.now + 3.years, "3 years"],
|
70
|
+
[Time.now, Time.now + 10.years, "10 years"],
|
71
|
+
[Time.now, Time.now + 3.hour, "3 hours"],
|
72
|
+
[Time.now, Time.now + 13.months, "1 year and 1 month"],
|
73
|
+
# Any numeric sequence is merely coincidental.
|
74
|
+
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, "1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds"],
|
75
|
+
["2009-3-16".to_time, "2008-4-14".to_time, "11 months and 2 days"],
|
76
|
+
["2009-3-16".to_time + 1.minute, "2008-4-14".to_time, "11 months, 2 days, and 1 minute"],
|
77
|
+
["2009-4-14".to_time, "2008-3-16".to_time, "1 year and 29 days"],
|
78
|
+
["2009-2-01".to_time, "2009-3-01".to_time, "1 month"],
|
79
|
+
["2008-2-01".to_time, "2008-3-01".to_time, "1 month"]
|
80
|
+
].each do |start, finish, output|
|
81
|
+
it "should be #{output}" do
|
82
|
+
distance_of_time_in_words(start, finish, true).should eql(output)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "with output options" do
|
88
|
+
[
|
89
|
+
# Any numeric sequence is merely coincidental.
|
90
|
+
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :except => "minutes" }, "1 year, 2 months, 3 days, 4 hours, and 6 seconds"],
|
91
|
+
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :words_connector => " - " }, "1 year - 2 months - 3 days - 4 hours - 5 minutes, and 6 seconds"],
|
92
|
+
[Time.now, Time.now + 5.minutes + 6.seconds, { :two_words_connector => " - " }, "5 minutes - 6 seconds"],
|
93
|
+
[Time.now, Time.now + 4.hours + 5.minutes + 6.seconds, { :last_word_connector => " - " }, "4 hours, 5 minutes - 6 seconds"],
|
94
|
+
[Time.now, Time.now + 1.hour + 1.minute, { :except => "minutes"}, "1 hour"],
|
95
|
+
[Time.now, Time.now + 1.hour + 1.day + 1.minute, { :except => ["minutes", "hours"]}, "1 day"],
|
96
|
+
[Time.now, Time.now + 1.hour + 1.day + 1.minute, { :only => ["minutes", "hours"]}, "1 hour and 1 minute"],
|
97
|
+
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :precision => 2 }, "1 year and 2 months"],
|
98
|
+
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :precision => 3 }, "1 year, 2 months, and 3 days"],
|
99
|
+
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :precision => 10 }, "1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds"]
|
100
|
+
].each do |start, finish, options, output|
|
101
|
+
it "should be #{output}" do
|
102
|
+
distance_of_time_in_words(start, finish, true, options).should eql(output)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../lib"))
|
3
|
+
|
4
|
+
require 'action_controller'
|
5
|
+
require 'activesupport'
|
6
|
+
require 'dotiw'
|
7
|
+
require 'spec'
|
8
|
+
|
9
|
+
# Define time zone before loading, just like in the real world
|
10
|
+
zone = "UTC"
|
11
|
+
Time.zone = zone
|
12
|
+
|
13
|
+
I18n.load_path << Dir[File.join(File.dirname(__FILE__), "translations", "*")]
|
14
|
+
I18n.locale = :en
|
15
|
+
|
16
|
+
# bootstraping the plugin through init.rb
|
17
|
+
# tests how it would load in a real application
|
18
|
+
load File.dirname(__FILE__) + "/../rails/init.rb"
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dotiw
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Bigg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-18 00:00:00 +10: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: "0"
|
24
|
+
version:
|
25
|
+
description: Better distance_of_time_in_words for Rails
|
26
|
+
email: radarlistener@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.markdown
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.markdown
|
37
|
+
- Rakefile
|
38
|
+
- VERSION
|
39
|
+
- dotiw.gemspec
|
40
|
+
- lib/dotiw.rb
|
41
|
+
- lib/i18n.rb
|
42
|
+
- rails/init.rb
|
43
|
+
- spec/dotiw_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/translations/en.yml
|
46
|
+
- spec/translations/es.yml
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/radar/dotiw
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --charset=UTF-8
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.5
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Better distance_of_time_in_words for Rails
|
75
|
+
test_files:
|
76
|
+
- spec/dotiw_spec.rb
|
77
|
+
- spec/spec_helper.rb
|