dotiw 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  tmp
2
+ pkg
2
3
  spec/fixtures/database.yml
data/README.markdown CHANGED
@@ -16,6 +16,13 @@ This takes the same options plus an additional one on the end for passing option
16
16
 
17
17
  Oh, and did I mention it supports I18n? Oh yeah.
18
18
 
19
+ ## distance\_of\_time
20
+
21
+ If you have simply a number of seconds you can get the "stringified" version of this by using `distance_of_time`:
22
+
23
+ >> distance_of_time(300)
24
+ => "5 minutes"
25
+
19
26
  ## distance\_of\_time\_in\_words\_hash
20
27
 
21
28
  Don't like any format you're given? That's cool too! Here, have an indifferent hash version:
@@ -47,6 +54,10 @@ The third argument for this method is whether or not to include seconds. By defa
47
54
 
48
55
  Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility".
49
56
 
57
+ #### :vague
58
+
59
+ Specify this if you want it to use the old `distance_of_time_in_words`. The value can be anything except `nil` or `false`.
60
+
50
61
  #### :locale
51
62
 
52
63
  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):
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
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 `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
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.0"
8
+ s.version = "0.3.1"
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{2009-10-18}
12
+ s.date = %q{2009-12-12}
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 = [
@@ -24,7 +24,6 @@ Gem::Specification.new do |s|
24
24
  "dotiw.gemspec",
25
25
  "lib/dotiw.rb",
26
26
  "lib/i18n.rb",
27
- "pkg/dotiw-0.2.4.gem",
28
27
  "rails/init.rb",
29
28
  "spec/dotiw_spec.rb",
30
29
  "spec/spec_helper.rb",
@@ -54,3 +53,4 @@ Gem::Specification.new do |s|
54
53
  s.add_dependency(%q<rspec>, [">= 0"])
55
54
  end
56
55
  end
56
+
data/lib/dotiw.rb CHANGED
@@ -1,12 +1,19 @@
1
+ # encoding: utf-8
1
2
  module ActionView
2
3
  module Helpers
3
4
  module DateHelper
4
5
  def distance_of_time_in_words_hash(from_time, to_time, options={})
5
- output = HashWithIndifferentAccess.new
6
6
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
7
7
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
8
8
 
9
9
  distance = (from_time - to_time).abs
10
+ distance_of_time_hash(distance, from_time, to_time, options)
11
+ end
12
+
13
+ def distance_of_time_hash(distance, from_time = nil, to_time = nil, options={})
14
+ output = HashWithIndifferentAccess.new
15
+ from_time ||= Time.now
16
+ to_time ||= from_time + distance.seconds
10
17
  I18n.with_options :locale => options[:locale] do |locale|
11
18
  while distance > 0
12
19
  if distance < 1.minute
@@ -34,7 +41,7 @@ module ActionView
34
41
  # Convert the last month to days and add to total
35
42
  months -= 1
36
43
  last_month = largest.advance(:months => -1)
37
- days += Time.days_in_month(last_month.month, last_month.year)
44
+ days += Time.days_in_month(last_month.month, last_month.year)
38
45
  end
39
46
 
40
47
  if months < 0
@@ -54,8 +61,13 @@ module ActionView
54
61
  output
55
62
  end
56
63
 
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)
64
+ alias_method :old_distance_of_time_in_words, :distance_of_time_in_words
65
+
66
+ def distance_of_time(seconds, options = {})
67
+ display_time_in_words(distance_of_time_hash(seconds), options)
68
+ end
69
+
70
+ def display_time_in_words(hash, include_seconds = false, options = {})
59
71
  hash.delete(:seconds) if !include_seconds
60
72
  I18n.with_options :locale => options[:locale] do |locale|
61
73
  # Remove all the values that are nil.
@@ -87,8 +99,15 @@ module ActionView
87
99
  options.delete(:precision)
88
100
  end
89
101
 
102
+
90
103
  output.to_sentence(options)
91
- end
104
+ end
105
+ end
106
+
107
+ def distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {})
108
+ return old_distance_of_time_in_words(from_time, to_time, include_seconds, options) if options.delete(:vague)
109
+ hash = distance_of_time_in_words_hash(from_time, to_time, options)
110
+ display_time_in_words(hash, include_seconds, options)
92
111
  end
93
112
  end
94
113
  end
data/spec/dotiw_spec.rb CHANGED
@@ -12,6 +12,20 @@ describe "A better distance_of_time_in_words" do
12
12
  Time.zone.stub!(:now).and_return(time)
13
13
  end
14
14
 
15
+ describe "distance of time" do
16
+ [
17
+ [5.minutes.to_i, "5 minutes"],
18
+ [10.minutes.to_i, "10 minutes"],
19
+ [1.hour.to_i, "1 hour"],
20
+ [4.weeks.to_i, "4 weeks"],
21
+ [24.weeks.to_i, "5 months and 15 days"],
22
+ ].each do |number, result|
23
+ it "#{number} == #{result}" do
24
+ distance_of_time(number).should eql(result)
25
+ end
26
+ end
27
+ end
28
+
15
29
  describe "hash version" do
16
30
  describe "giving correct numbers of" do
17
31
 
@@ -87,16 +101,26 @@ describe "A better distance_of_time_in_words" do
87
101
  describe "with output options" do
88
102
  [
89
103
  # 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
104
  [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"],
105
+
92
106
  [Time.now, Time.now + 5.minutes + 6.seconds, { :two_words_connector => " - " }, "5 minutes - 6 seconds"],
107
+
93
108
  [Time.now, Time.now + 4.hours + 5.minutes + 6.seconds, { :last_word_connector => " - " }, "4 hours, 5 minutes - 6 seconds"],
109
+
110
+ [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"],
94
111
  [Time.now, Time.now + 1.hour + 1.minute, { :except => "minutes"}, "1 hour"],
95
112
  [Time.now, Time.now + 1.hour + 1.day + 1.minute, { :except => ["minutes", "hours"]}, "1 day"],
113
+
96
114
  [Time.now, Time.now + 1.hour + 1.day + 1.minute, { :only => ["minutes", "hours"]}, "1 hour and 1 minute"],
115
+
97
116
  [Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :precision => 2 }, "1 year and 2 months"],
98
117
  [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"]
118
+ [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"],
119
+
120
+ [Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :vague => true }, "about 1 year"],
121
+ [Time.now, Time.now + 1.year + 2.months + 3.days + 4.hours + 5.minutes + 6.seconds, { :vague => "Yes please" }, "about 1 year"],
122
+ [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
+ [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"],
100
124
  ].each do |start, finish, options, output|
101
125
  it "should be #{output}" do
102
126
  distance_of_time_in_words(start, finish, true, options).should eql(output)
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.0
4
+ version: 0.3.1
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: 2009-10-18 00:00:00 +10:00
12
+ date: 2009-12-12 00:00:00 +10:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -39,7 +39,6 @@ files:
39
39
  - dotiw.gemspec
40
40
  - lib/dotiw.rb
41
41
  - lib/i18n.rb
42
- - pkg/dotiw-0.2.4.gem
43
42
  - rails/init.rb
44
43
  - spec/dotiw_spec.rb
45
44
  - spec/spec_helper.rb
data/pkg/dotiw-0.2.4.gem DELETED
Binary file