dotiw 0.6.0 → 0.7.0
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/Gemfile.lock +1 -4
- data/README.markdown +38 -35
- data/lib/dotiw/version.rb +2 -2
- data/lib/dotiw.rb +42 -39
- data/spec/lib/dotiw_spec.rb +4 -0
- metadata +8 -2
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -18,60 +18,47 @@ Better than "about 1 year", am I right? Of course I am.
|
|
18
18
|
>> distance_of_time_in_words(Time.now, Time.now + 1.second, false)
|
19
19
|
=> "1 second"
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
Oh, and did I mention it supports I18n? Oh yeah.
|
24
|
-
|
25
|
-
## distance\_of\_time
|
26
|
-
|
27
|
-
If you have simply a number of seconds you can get the "stringified" version of this by using `distance_of_time`:
|
28
|
-
|
29
|
-
>> distance_of_time(300)
|
30
|
-
=> "5 minutes"
|
21
|
+
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:
|
31
22
|
|
32
|
-
|
23
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 1.second, true)
|
24
|
+
=> "1 year, and 1 second"
|
33
25
|
|
34
|
-
|
26
|
+
Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility".
|
35
27
|
|
36
|
-
|
37
|
-
=> {"days"=>3, "seconds"=>6, "minutes"=>5, "years"=>1, "hours"=>4, "months"=>2}
|
28
|
+
The last argument is an optional options hash that can be used to manipulate behavior and (which uses `to_sentence`).
|
38
29
|
|
39
|
-
|
30
|
+
Oh, and did I mention it supports I18n? Oh yeah.
|
40
31
|
|
41
32
|
### Options
|
42
33
|
|
43
34
|
#### :locale
|
44
35
|
|
45
|
-
|
36
|
+
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):
|
46
37
|
|
47
|
-
>>
|
48
|
-
=>
|
38
|
+
>> distance_of_time_in_words(Time.now, Time.now + 1.minute, false, :locale => "es")
|
39
|
+
=> "1 minuto"
|
49
40
|
|
50
|
-
|
41
|
+
This will also be passed to `to_sentence`
|
51
42
|
|
52
|
-
|
43
|
+
#### :vague
|
53
44
|
|
54
|
-
|
45
|
+
Specify this if you want it to use the old `distance_of_time_in_words`. The value can be anything except `nil` or `false`.
|
55
46
|
|
56
|
-
|
47
|
+
#### :singularize
|
57
48
|
|
58
|
-
|
59
|
-
=> "1 year, and 1 second"
|
60
|
-
|
61
|
-
Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility".
|
49
|
+
Specify if all values of the hash should be presented in their singular form. By default they will be pluralized whenever outside the `-1..1` range. If you wish to have them signularized, just add the option `:singularize => :always`.
|
62
50
|
|
63
|
-
|
51
|
+
This option is useful for Russian and Icelandic folks (https://github.com/radar/dotiw/issues#issue/2).
|
64
52
|
|
65
|
-
|
53
|
+
>> distance_of_time_in_words(Time.now, Time.now + 2.hour + 2.minute, true, :singularize => :always)
|
54
|
+
=> "2 hour and 2 minute"
|
66
55
|
|
67
|
-
#### :
|
56
|
+
#### :accumulate_on
|
68
57
|
|
69
|
-
|
58
|
+
Specifies the maximum output unit which will accumulate all the surplus. Say you set it to seconds and your time difference is of 2 minutes then the output would be 120 seconds. Here's a code example:
|
70
59
|
|
71
|
-
>> distance_of_time_in_words(Time.now, Time.now +
|
72
|
-
=> "
|
73
|
-
|
74
|
-
This will also be passed to `to_sentence`
|
60
|
+
>> distance_of_time_in_words(Time.now, Time.now + 2.hour + 70.second, true, :accumulate_on => :minutes)
|
61
|
+
=> "121 minutes minute and 10 seconds"
|
75
62
|
|
76
63
|
#### :only
|
77
64
|
|
@@ -127,6 +114,22 @@ Using something other than ', and':
|
|
127
114
|
|
128
115
|
>> distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute + 1.second, true, { :last_word_connector => ', finally ' })
|
129
116
|
=> "1 hour, 1 minute, finally 1 second"
|
117
|
+
|
118
|
+
## distance\_of\_time
|
119
|
+
|
120
|
+
If you have simply a number of seconds you can get the "stringified" version of this by using `distance_of_time`:
|
121
|
+
|
122
|
+
>> distance_of_time(300)
|
123
|
+
=> "5 minutes"
|
124
|
+
|
125
|
+
## distance\_of\_time\_in\_words\_hash
|
126
|
+
|
127
|
+
Don't like any format you're given? That's cool too! Here, have an indifferent hash version:
|
128
|
+
|
129
|
+
>> distance_of_time_in_words_hash(Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds)
|
130
|
+
=> {"days"=>3, "seconds"=>6, "minutes"=>5, "years"=>1, "hours"=>4, "months"=>2}
|
131
|
+
|
132
|
+
Indifferent means that you can access all keys by their `String` or `Symbol` version.
|
130
133
|
|
131
134
|
## distance\_of\_time\_in\_percent
|
132
135
|
|
@@ -139,4 +142,4 @@ If you want to calculate a distance of time in percent, use `distance_of_time_in
|
|
139
142
|
|
140
143
|
* [chendo][http://github.com/chendo] - for talking through it with me and drawing on the whiteboard
|
141
144
|
* [Derander][http://github.com/derander] - correct Spanish translations
|
142
|
-
* [DBA][http://github.com/dba] - Commits leading up to the 0.
|
145
|
+
* [DBA][http://github.com/dba] - Commits leading up to the 0.7 release.
|
data/lib/dotiw/version.rb
CHANGED
data/lib/dotiw.rb
CHANGED
@@ -21,45 +21,6 @@ module ActionView
|
|
21
21
|
display_time_in_words DOTIW::TimeHash.new(seconds).to_hash, options
|
22
22
|
end
|
23
23
|
|
24
|
-
def display_time_in_words(hash, include_seconds = false, options = {})
|
25
|
-
options.symbolize_keys!
|
26
|
-
I18n.locale = options[:locale] if options[:locale]
|
27
|
-
|
28
|
-
time_measurements = { :years => I18n.t(:years, :default => "years"),
|
29
|
-
:months => I18n.t(:months, :default => "months"),
|
30
|
-
:weeks => I18n.t(:weeks, :default => "weeks"),
|
31
|
-
:days => I18n.t(:days, :default => "days"),
|
32
|
-
:hours => I18n.t(:hours, :default => "hours"),
|
33
|
-
:minutes => I18n.t(:minutes, :default => "minutes"),
|
34
|
-
:seconds => I18n.t(:seconds, :default => "seconds") }
|
35
|
-
|
36
|
-
hash.delete(time_measurements[:seconds]) if !include_seconds && hash[time_measurements[:minutes]]
|
37
|
-
|
38
|
-
# Remove all the values that are nil or excluded. Keep the required ones.
|
39
|
-
time_measurements.delete_if do |measure, key|
|
40
|
-
hash[key].nil? || hash[key].zero? || (!options[:except].nil? && options[:except].include?(key)) ||
|
41
|
-
(options[:only] && !options[:only].include?(key))
|
42
|
-
end
|
43
|
-
|
44
|
-
options.delete(:except)
|
45
|
-
options.delete(:only)
|
46
|
-
|
47
|
-
output = []
|
48
|
-
|
49
|
-
time_measurements.each do |measure, key|
|
50
|
-
name = hash[key] > 1 ? key : key.singularize
|
51
|
-
output += ["#{hash[key]} #{name}"]
|
52
|
-
end
|
53
|
-
|
54
|
-
# maybe only grab the first few values
|
55
|
-
if options[:precision]
|
56
|
-
output = output[0...options[:precision]]
|
57
|
-
options.delete(:precision)
|
58
|
-
end
|
59
|
-
|
60
|
-
output.to_sentence(options)
|
61
|
-
end
|
62
|
-
|
63
24
|
def distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {})
|
64
25
|
return old_distance_of_time_in_words(from_time, to_time, include_seconds, options) if options.delete(:vague)
|
65
26
|
hash = distance_of_time_in_words_hash(from_time, to_time, options)
|
@@ -72,6 +33,48 @@ module ActionView
|
|
72
33
|
result = ((current_time - from_time) / distance) * 100
|
73
34
|
number_with_precision(result, options).to_s + "%"
|
74
35
|
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def display_time_in_words(hash, include_seconds = false, options = {})
|
39
|
+
options.symbolize_keys!
|
40
|
+
I18n.locale = options[:locale] if options[:locale]
|
41
|
+
|
42
|
+
time_measurements = { :years => I18n.t(:years, :default => "years"),
|
43
|
+
:months => I18n.t(:months, :default => "months"),
|
44
|
+
:weeks => I18n.t(:weeks, :default => "weeks"),
|
45
|
+
:days => I18n.t(:days, :default => "days"),
|
46
|
+
:hours => I18n.t(:hours, :default => "hours"),
|
47
|
+
:minutes => I18n.t(:minutes, :default => "minutes"),
|
48
|
+
:seconds => I18n.t(:seconds, :default => "seconds") }
|
49
|
+
|
50
|
+
hash.delete(time_measurements[:seconds]) if !include_seconds && hash[time_measurements[:minutes]]
|
51
|
+
|
52
|
+
# Remove all the values that are nil or excluded. Keep the required ones.
|
53
|
+
time_measurements.delete_if do |measure, key|
|
54
|
+
hash[key].nil? || hash[key].zero? || (!options[:except].nil? && options[:except].include?(key)) ||
|
55
|
+
(options[:only] && !options[:only].include?(key))
|
56
|
+
end
|
57
|
+
|
58
|
+
options.delete(:except)
|
59
|
+
options.delete(:only)
|
60
|
+
|
61
|
+
output = []
|
62
|
+
|
63
|
+
time_measurements.each do |measure, key|
|
64
|
+
name = options[:singularize] == :always || hash[key].between?(-1, 1) ? key.singularize : key
|
65
|
+
output += ["#{hash[key]} #{name}"]
|
66
|
+
end
|
67
|
+
|
68
|
+
options.delete(:singularize)
|
69
|
+
|
70
|
+
# maybe only grab the first few values
|
71
|
+
if options[:precision]
|
72
|
+
output = output[0...options[:precision]]
|
73
|
+
options.delete(:precision)
|
74
|
+
end
|
75
|
+
|
76
|
+
output.to_sentence(options)
|
77
|
+
end
|
75
78
|
end # DateHelper
|
76
79
|
end # Helpers
|
77
80
|
end # ActionView
|
data/spec/lib/dotiw_spec.rb
CHANGED
@@ -199,6 +199,10 @@ describe "A better distance_of_time_in_words" do
|
|
199
199
|
Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds,
|
200
200
|
{ "except" => "minutes" },
|
201
201
|
"1 year, 2 months, 3 days, 4 hours, and 6 seconds"],
|
202
|
+
[Time.now,
|
203
|
+
Time.now + 2.year + 3.months + 4.days + 5.hours + 6.minutes + 7.seconds,
|
204
|
+
{ :singularize => :always },
|
205
|
+
"2 year, 3 month, 4 day, 5 hour, 6 minute, and 7 second"]
|
202
206
|
].each do |start, finish, options, output|
|
203
207
|
it "should be #{output}" do
|
204
208
|
distance_of_time_in_words(start, finish, true, options).should eql(output)
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dotiw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 7
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.7.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Ryan Bigg
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ~>
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
28
30
|
segments:
|
29
31
|
- 3
|
30
32
|
- 0
|
@@ -40,6 +42,7 @@ dependencies:
|
|
40
42
|
requirements:
|
41
43
|
- - ~>
|
42
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
43
46
|
segments:
|
44
47
|
- 1
|
45
48
|
- 0
|
@@ -55,6 +58,7 @@ dependencies:
|
|
55
58
|
requirements:
|
56
59
|
- - ~>
|
57
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
58
62
|
segments:
|
59
63
|
- 2
|
60
64
|
- 0
|
@@ -101,6 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
105
|
requirements:
|
102
106
|
- - ">="
|
103
107
|
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
104
109
|
segments:
|
105
110
|
- 0
|
106
111
|
version: "0"
|
@@ -109,6 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
114
|
requirements:
|
110
115
|
- - ">="
|
111
116
|
- !ruby/object:Gem::Version
|
117
|
+
hash: 23
|
112
118
|
segments:
|
113
119
|
- 1
|
114
120
|
- 3
|