dotiw 0.3.1 → 0.3.2
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/README.markdown +7 -0
- data/VERSION +1 -1
- data/dotiw.gemspec +4 -5
- data/lib/dotiw.rb +7 -0
- data/spec/dotiw_spec.rb +24 -3
- data/spec/spec_helper.rb +2 -1
- metadata +2 -2
data/README.markdown
CHANGED
@@ -122,6 +122,13 @@ Using something other than ', and':
|
|
122
122
|
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, { :last_word_connector => ', finally ' })
|
123
123
|
=> "1 hour, 1 minute, finally 1 second"
|
124
124
|
|
125
|
+
## distance\_of\_time\_in\_percent
|
126
|
+
|
127
|
+
If you want to calculate a distance of time in percent, use `distance_of_time_in_percent`. The first argument is the beginning time, the second argument the "current" time and the third argument is the end time. This method takes the same options as [`number_with_precision`][http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001687].
|
128
|
+
|
129
|
+
distance_of_time_in_percent("04-12-2009".to_time, "29-01-2010".to_time, "04-12-2010".to_time, options)
|
130
|
+
|
131
|
+
|
125
132
|
## Contributors
|
126
133
|
|
127
134
|
chendo - for talking through it with me and drawing on the whiteboard
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/dotiw.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{dotiw}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Bigg"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-01-29}
|
13
13
|
s.description = %q{Better distance_of_time_in_words for Rails}
|
14
14
|
s.email = %q{radarlistener@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -53,4 +53,3 @@ Gem::Specification.new do |s|
|
|
53
53
|
s.add_dependency(%q<rspec>, [">= 0"])
|
54
54
|
end
|
55
55
|
end
|
56
|
-
|
data/lib/dotiw.rb
CHANGED
@@ -68,6 +68,7 @@ module ActionView
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def display_time_in_words(hash, include_seconds = false, options = {})
|
71
|
+
options.symbolize_keys!
|
71
72
|
hash.delete(:seconds) if !include_seconds
|
72
73
|
I18n.with_options :locale => options[:locale] do |locale|
|
73
74
|
# Remove all the values that are nil.
|
@@ -109,6 +110,12 @@ module ActionView
|
|
109
110
|
hash = distance_of_time_in_words_hash(from_time, to_time, options)
|
110
111
|
display_time_in_words(hash, include_seconds, options)
|
111
112
|
end
|
113
|
+
|
114
|
+
def distance_of_time_in_percent(from_time, current_time, to_time, options = {})
|
115
|
+
options[:precision] ||= 0
|
116
|
+
distance = to_time - from_time
|
117
|
+
number_with_precision(((current_time - from_time) / distance) * 100, options).to_s + "%"
|
118
|
+
end
|
112
119
|
end
|
113
120
|
end
|
114
121
|
end
|
data/spec/dotiw_spec.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
3
|
require 'dotiw'
|
3
4
|
|
4
5
|
describe "A better distance_of_time_in_words" do
|
5
6
|
include ActionView::Helpers::DateHelper
|
6
7
|
include ActionView::Helpers::TextHelper
|
8
|
+
include ActionView::Helpers::NumberHelper
|
7
9
|
|
8
10
|
before do
|
9
11
|
I18n.locale = :en
|
@@ -17,8 +19,8 @@ describe "A better distance_of_time_in_words" do
|
|
17
19
|
[5.minutes.to_i, "5 minutes"],
|
18
20
|
[10.minutes.to_i, "10 minutes"],
|
19
21
|
[1.hour.to_i, "1 hour"],
|
20
|
-
[4.weeks.to_i, "
|
21
|
-
[24.weeks.to_i, "5 months and 15 days"]
|
22
|
+
[4.weeks.to_i, "28 days"],
|
23
|
+
[24.weeks.to_i, "5 months and 15 days"]
|
22
24
|
].each do |number, result|
|
23
25
|
it "#{number} == #{result}" do
|
24
26
|
distance_of_time(number).should eql(result)
|
@@ -83,7 +85,9 @@ describe "A better distance_of_time_in_words" do
|
|
83
85
|
[Time.now, Time.now + 3.years, "3 years"],
|
84
86
|
[Time.now, Time.now + 10.years, "10 years"],
|
85
87
|
[Time.now, Time.now + 3.hour, "3 hours"],
|
86
|
-
|
88
|
+
# Need to be +1.day because it will output "1 year and 30 days" otherwise.
|
89
|
+
# Haven't investigated fully how this is caused.
|
90
|
+
[Time.now, Time.now + 13.months + 1.day, "1 year and 1 month"],
|
87
91
|
# Any numeric sequence is merely coincidental.
|
88
92
|
[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"],
|
89
93
|
["2009-3-16".to_time, "2008-4-14".to_time, "11 months and 2 days"],
|
@@ -121,11 +125,28 @@ describe "A better distance_of_time_in_words" do
|
|
121
125
|
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :vague => "Yes please" }, "about 1 year"],
|
122
126
|
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :vague => false }, "1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds"],
|
123
127
|
[Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :vague => nil }, "1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds"],
|
128
|
+
[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"],
|
124
129
|
].each do |start, finish, options, output|
|
125
130
|
it "should be #{output}" do
|
126
131
|
distance_of_time_in_words(start, finish, true, options).should eql(output)
|
127
132
|
end
|
128
133
|
end
|
129
134
|
end
|
135
|
+
|
136
|
+
describe "percentage of time" do
|
137
|
+
|
138
|
+
def time_in_percent(options = {})
|
139
|
+
distance_of_time_in_percent("04-12-2009".to_time, "29-01-2010".to_time, "04-12-2010".to_time, options)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "calculates 15%" do
|
143
|
+
time_in_percent.should eql("15%")
|
144
|
+
end
|
145
|
+
|
146
|
+
it "calculates 15.3%" do
|
147
|
+
time_in_percent(:precision => 1).should eql("15.3%")
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
130
151
|
|
131
152
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dotiw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Bigg
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-29 00:00:00 +10:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|