time_will_tell 0.0.3 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +90 -7
- data/lib/generators/time_will_tell/install/install_generator.rb +1 -0
- data/lib/generators/time_will_tell/install/templates/config/locales/time_will_tell.en.yml +4 -3
- data/lib/time_will_tell/helpers/date_range_helper.rb +8 -1
- data/lib/time_will_tell/version.rb +1 -1
- data/spec/fixtures/locales/time_will_tell.fr.yml +4 -3
- data/spec/helpers/date_range_helper_spec.rb +31 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5a680687c83bfbc5595591eed660e67f9bf6af1
|
4
|
+
data.tar.gz: ebbe5e4ffb9981daa57889fbc6bc5c94f290af56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4083a53a1234ac98932b8d8b58289dee948369873b1e3e179447c611990514acd8e992d670b93247888ce94f588b4a1d9fb4dd6f230fa2f912c8fefa2947f027
|
7
|
+
data.tar.gz: 2697182c903ade85536a061facb3b542d648ff2013a8b76a53b58b979812155ea00ed0799bf11d0b3d1ac90c62255d975d79840947a1eeb4c596926641a515d1
|
data/README.md
CHANGED
@@ -4,21 +4,104 @@ Specialized conversions of dates and times to strings.
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
Add
|
7
|
+
Add `time_will_tell` to your Gemfile:
|
8
8
|
|
9
9
|
gem 'time_will_tell'
|
10
10
|
|
11
|
-
|
11
|
+
### Install the default locale file
|
12
12
|
|
13
|
-
|
13
|
+
Run the generator to add the default locale file `time_will_tell.en.yml` to your `config/locales` directory:
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
$ gem install time_will_tell
|
15
|
+
$ rails generate time_will_tell:install
|
18
16
|
|
19
17
|
## Usage
|
20
18
|
|
21
|
-
|
19
|
+
`time_will_tell` offers 2 view helpers for dealing with dates.
|
20
|
+
|
21
|
+
### exact_distance_of_time_in_words
|
22
|
+
|
23
|
+
The similarity with [`distance_of_time_in_words`](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words) is not a coincidence. Both methods share the a similar signature:
|
24
|
+
|
25
|
+
exact_distance_of_time_in_words(from_time, to_time, options = {})
|
26
|
+
|
27
|
+
# => '2 hours 33 minutes'
|
28
|
+
# => '1 hour and a half'
|
29
|
+
# => '3 years 11 months and 21 seconds'
|
30
|
+
# => '1 month 15 days and 1 minute'
|
31
|
+
# => '1 day 2 hours and a half and 5 seconds'
|
32
|
+
# => 'half an hour'
|
33
|
+
|
34
|
+
#### Options
|
35
|
+
|
36
|
+
Displaying the seconds is optional and off by default. Use `include_seconds: true` to include them.
|
37
|
+
|
38
|
+
You can also supply your own locale by specifying a locale `scope`. The default is `time_will_tell.distance_in_words`.
|
39
|
+
|
40
|
+
#### Locales
|
41
|
+
|
42
|
+
You can either replace the default locale strings or copy the structure under a different scope to be able to toggle between a default version and an alternate one.
|
43
|
+
|
44
|
+
For example, the following scope would output
|
45
|
+
|
46
|
+
```
|
47
|
+
compact_distance_in_words:
|
48
|
+
template: "%{count}%{unit}"
|
49
|
+
template_long: "%{rest} %{last}"
|
50
|
+
|
51
|
+
units:
|
52
|
+
second: s
|
53
|
+
minute: m
|
54
|
+
hour: h
|
55
|
+
day: d
|
56
|
+
month: M
|
57
|
+
year: Y
|
58
|
+
|
59
|
+
special:
|
60
|
+
half_an_hour: 30m
|
61
|
+
and_a_half: 30m
|
62
|
+
```
|
63
|
+
|
64
|
+
exact_distance_of_time_in_words(from_time, to_time, scope: 'compact_distance_in_words')
|
65
|
+
# => '1d 2h 30m 5s'
|
66
|
+
|
67
|
+
### date_range
|
68
|
+
|
69
|
+
`date_range` simplifies displaying a date range in a brief format.
|
70
|
+
|
71
|
+
# Oct 3 - 8, 2012
|
72
|
+
# Jan 30 - Feb 5, 2013
|
73
|
+
# Dec 26, 2012 - Jan 3, 2013
|
74
|
+
|
75
|
+
The output depends on whether the dates are on the same day, month and year.
|
76
|
+
|
77
|
+
#### Options
|
78
|
+
|
79
|
+
##### format
|
80
|
+
|
81
|
+
You can choose to display the full month names by specifying `format: :long`.
|
82
|
+
|
83
|
+
date_range(from_date, to_date, format: :long)
|
84
|
+
# January 30 - February 5, 2013
|
85
|
+
|
86
|
+
##### separator
|
87
|
+
|
88
|
+
You can choose a different separator if you don't like this guy `-`.
|
89
|
+
|
90
|
+
date_range(from_date, to_date, separator: 'to')
|
91
|
+
# Oct 3 to 8, 2012
|
92
|
+
|
93
|
+
##### show_year
|
94
|
+
|
95
|
+
Sometimes you don't want the year to show up in the date range.
|
96
|
+
|
97
|
+
date_range(from_date, to_date, show_year: false)
|
98
|
+
# Jan 30 - Feb 5
|
99
|
+
|
100
|
+
By default the year will always be shown. Also, if the dates are in different years, the years will be shown even if `show_year` is `false`.
|
101
|
+
|
102
|
+
#### Locales
|
103
|
+
|
104
|
+
Just as with `exact_distance_of_time_in_words` you can specify your own locale `scope`.
|
22
105
|
|
23
106
|
## Contributing
|
24
107
|
|
@@ -1,10 +1,11 @@
|
|
1
1
|
en:
|
2
2
|
time_will_tell:
|
3
3
|
date_range:
|
4
|
-
different_months_same_year: "%{from_month} %{from_day} %{sep} %{to_month} %{to_day}
|
4
|
+
different_months_same_year: "%{from_month} %{from_day} %{sep} %{to_month} %{to_day}"
|
5
5
|
different_years: "%{from_month} %{from_day}, %{from_year} %{sep} %{to_month} %{to_day}, %{to_year}"
|
6
|
-
same_month: "%{month} %{from_day} %{sep} %{to_day}
|
7
|
-
same_date: "%{month} %{from_day}
|
6
|
+
same_month: "%{month} %{from_day} %{sep} %{to_day}"
|
7
|
+
same_date: "%{month} %{from_day}"
|
8
|
+
with_year: "%{date_range}, %{year}"
|
8
9
|
|
9
10
|
distance_in_words:
|
10
11
|
template: "%{count} %{unit}"
|
@@ -6,6 +6,7 @@ module TimeWillTell
|
|
6
6
|
format = options.fetch(:format, :short)
|
7
7
|
scope = options.fetch(:scope, 'time_will_tell.date_range')
|
8
8
|
separator = options.fetch(:separator, '—')
|
9
|
+
show_year = options.fetch(:show_year, true)
|
9
10
|
|
10
11
|
month_names = format.to_sym == :short ? I18n.t("date.abbr_month_names") : I18n.t("date.month_names")
|
11
12
|
|
@@ -39,7 +40,13 @@ module TimeWillTell
|
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
42
|
-
I18n.t("#{scope}.#{template}", dates)
|
43
|
+
without_year = I18n.t("#{scope}.#{template}", dates)
|
44
|
+
|
45
|
+
if show_year && from_date.year == to_date.year
|
46
|
+
I18n.t("#{scope}.with_year", date_range: without_year, year: from_year, default: without_year)
|
47
|
+
else
|
48
|
+
without_year
|
49
|
+
end
|
43
50
|
end
|
44
51
|
|
45
52
|
end
|
@@ -31,10 +31,11 @@ fr:
|
|
31
31
|
|
32
32
|
time_will_tell:
|
33
33
|
date_range:
|
34
|
-
different_months_same_year: "%{from_day} %{from_month} %{sep} %{to_day} %{to_month}
|
34
|
+
different_months_same_year: "%{from_day} %{from_month} %{sep} %{to_day} %{to_month}"
|
35
35
|
different_years: "%{from_day} %{from_month} %{from_year} %{sep} %{to_day} %{to_month} %{to_year}"
|
36
|
-
same_month: "%{from_day} %{sep} %{to_day} %{month}
|
37
|
-
same_date: "%{from_day} %{month}
|
36
|
+
same_month: "%{from_day} %{sep} %{to_day} %{month}"
|
37
|
+
same_date: "%{from_day} %{month}"
|
38
|
+
with_year: "%{date_range} %{year}"
|
38
39
|
|
39
40
|
distance_in_words:
|
40
41
|
template: "%{count} %{unit}"
|
@@ -11,18 +11,18 @@ describe TimeWillTell::Helpers::DateRangeHelper do
|
|
11
11
|
describe '#date_range' do
|
12
12
|
{
|
13
13
|
# short form
|
14
|
-
[Date.new(2012, 10, 3),
|
15
|
-
[Date.new(2013, 1, 30),
|
16
|
-
[Date.new(2012, 12, 26), Date.new(2013, 1, 3)]
|
17
|
-
[Date.new(2013, 8, 24),
|
18
|
-
[Date.new(2014, 2, 10),
|
14
|
+
[Date.new(2012, 10, 3), Date.new(2012, 10, 8)] => 'Oct 3 — 8, 2012',
|
15
|
+
[Date.new(2013, 1, 30), Date.new(2013, 2, 5)] => 'Jan 30 — Feb 5, 2013',
|
16
|
+
[Date.new(2012, 12, 26), Date.new(2013, 1, 3)] => 'Dec 26, 2012 — Jan 3, 2013',
|
17
|
+
[Date.new(2013, 8, 24), Date.new(2013, 8, 24)] => 'Aug 24, 2013',
|
18
|
+
[Date.new(2014, 2, 10), Date.new(2015, 2, 10)] => 'Feb 10, 2014 — Feb 10, 2015',
|
19
19
|
|
20
20
|
# long form
|
21
|
-
[Date.new(2012, 10, 3),
|
22
|
-
[Date.new(2013, 1, 30),
|
23
|
-
[Date.new(2012, 12, 26), Date.new(2013, 1, 3),
|
24
|
-
[Date.new(2013, 8, 24),
|
25
|
-
[Date.new(2014, 2, 10),
|
21
|
+
[Date.new(2012, 10, 3), Date.new(2012, 10, 8), format: :long] => 'October 3 — 8, 2012',
|
22
|
+
[Date.new(2013, 1, 30), Date.new(2013, 2, 5), format: :long] => 'January 30 — February 5, 2013',
|
23
|
+
[Date.new(2012, 12, 26), Date.new(2013, 1, 3), format: :long] => 'December 26, 2012 — January 3, 2013',
|
24
|
+
[Date.new(2013, 8, 24), Date.new(2013, 8, 24), format: :long] => 'August 24, 2013',
|
25
|
+
[Date.new(2014, 2, 10), Date.new(2015, 2, 10), format: :long] => 'February 10, 2014 — February 10, 2015',
|
26
26
|
}.each do |args, expected|
|
27
27
|
it "outputs '#{expected}" do
|
28
28
|
from_date = args[0]
|
@@ -53,6 +53,27 @@ describe TimeWillTell::Helpers::DateRangeHelper do
|
|
53
53
|
).to eq 'Oct 31 ~ Dec 25, 2013'
|
54
54
|
end
|
55
55
|
|
56
|
+
context 'when show_year option is false' do
|
57
|
+
let(:options) { { show_year: false } }
|
58
|
+
|
59
|
+
subject { helper.date_range(from_date, to_date, options) }
|
60
|
+
|
61
|
+
{
|
62
|
+
[Date.new(2012, 10, 3), Date.new(2012, 10, 8)] => 'Oct 3 — 8',
|
63
|
+
[Date.new(2013, 1, 30), Date.new(2013, 2, 5)] => 'Jan 30 — Feb 5',
|
64
|
+
[Date.new(2012, 12, 26), Date.new(2013, 1, 3)] => 'Dec 26, 2012 — Jan 3, 2013',
|
65
|
+
[Date.new(2013, 8, 24), Date.new(2013, 8, 24)] => 'Aug 24',
|
66
|
+
[Date.new(2014, 2, 10), Date.new(2015, 2, 10)] => 'Feb 10, 2014 — Feb 10, 2015',
|
67
|
+
}.each do |args, expected|
|
68
|
+
context "from #{args[0]} to #{args[1]}" do
|
69
|
+
let(:from_date) { args[0] }
|
70
|
+
let(:to_date) { args[1] }
|
71
|
+
|
72
|
+
it { should eq expected }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end # when show_year option is false
|
76
|
+
|
56
77
|
context 'with a custom locale' do
|
57
78
|
let(:locale) { :fr }
|
58
79
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_will_tell
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Billard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
112
|
version: '0'
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project:
|
115
|
-
rubygems_version: 2.0
|
115
|
+
rubygems_version: 2.3.0
|
116
116
|
signing_key:
|
117
117
|
specification_version: 4
|
118
118
|
summary: Specialized conversions of dates and times to strings.
|