precise_distance_of_time_in_words 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -9
- data/lib/precise_distance_of_time_in_words.rb +24 -13
- data/lib/precise_distance_of_time_in_words/locales/de.yml +13 -0
- data/lib/precise_distance_of_time_in_words/locales/en.yml +13 -0
- data/lib/precise_distance_of_time_in_words/version.rb +1 -1
- data/precise_distance_of_time_in_words.gemspec +1 -1
- data/spec/precise_distance_of_time_in_words_spec.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d25a687e5063796fa37d0dfd752d0c9457ecc2e
|
4
|
+
data.tar.gz: 2b80e575d14c0f54d6da86d03115f154d399a672
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e509e143d85a514c407f8ff5abbbfdb563946b8fd0acdd5b6379ca589ab1300d34a99241a72983b71378bb6453d4d36edeaf484d022f045b62aa1abb577f9e3
|
7
|
+
data.tar.gz: a2c750241b44862c82e07b073f8583d98da4f4a815eaca0028390e8ae125e19960421614c8c104361958800c866a1d0697e7cc47fa5be4a3cdb8eeb14d6a166f
|
data/README.md
CHANGED
@@ -2,27 +2,61 @@
|
|
2
2
|
|
3
3
|
This gem offers a more precise "distance_of_time_in_words"
|
4
4
|
|
5
|
+
|
5
6
|
## Installation
|
6
7
|
|
7
8
|
Add this line to your application's Gemfile:
|
8
9
|
|
9
|
-
|
10
|
+
``` ruby
|
11
|
+
gem 'precise_distance_of_time_in_words'
|
12
|
+
```
|
10
13
|
|
11
14
|
And then execute:
|
12
15
|
|
13
|
-
|
16
|
+
``` shell
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Or install it standalone:
|
14
21
|
|
15
|
-
|
22
|
+
``` shell
|
23
|
+
$ gem install precise_distance_of_time_in_words
|
24
|
+
```
|
16
25
|
|
17
|
-
$ gem install precise_distance_of_time_in_words
|
18
26
|
|
19
27
|
## Usage
|
20
28
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
29
|
+
This gem offers you the ```precise_distance_of_time_in_words(from, to)"``` method, that you can
|
30
|
+
use anywhere (views, helpers, controllers, models, console). For exemple:
|
31
|
+
|
32
|
+
``` ruby
|
33
|
+
precise_distance_of_time_in_words(0, 1) # return '1 second'
|
34
|
+
precise_distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute) # return '1 hour and 1 minute'
|
35
|
+
```
|
36
|
+
|
37
|
+
## I18n
|
38
|
+
|
39
|
+
The default labels for 'hours', 'minutes', 'seconds' and 'and' are stored in the
|
40
|
+
I18n yaml inside the gem. You can switch the value per I18n.locale for your
|
41
|
+
internationalized application. Keys and the default values are the following.
|
42
|
+
You can override them by adding to a YAML file in your <tt>Rails.root/config/locales</tt>
|
43
|
+
directory.
|
44
|
+
|
45
|
+
``` yml
|
46
|
+
en:
|
47
|
+
datetime:
|
48
|
+
precise_distance_in_words:
|
49
|
+
x_minutes:
|
50
|
+
one: '%{count} minute'
|
51
|
+
other: '%{count} minutes'
|
52
|
+
x_seconds:
|
53
|
+
one: '%{count} second'
|
54
|
+
other: '%{count} seconds'
|
55
|
+
x_hours:
|
56
|
+
one: '%{count} hour'
|
57
|
+
other: '%{count} hours'
|
58
|
+
and: and
|
59
|
+
```
|
26
60
|
|
27
61
|
## Contributing
|
28
62
|
|
@@ -1,7 +1,18 @@
|
|
1
1
|
require "precise_distance_of_time_in_words/version"
|
2
2
|
|
3
3
|
module PreciseDistanceOfTimeInWords
|
4
|
-
def precise_distance_of_time_in_words(from_time, to_time)
|
4
|
+
def precise_distance_of_time_in_words(from_time, to_time=0, options = {})
|
5
|
+
options = {
|
6
|
+
scope: :'datetime.precise_distance_in_words'
|
7
|
+
}.merge!(options)
|
8
|
+
|
9
|
+
# using to_time casting method of objects when presented
|
10
|
+
from_time = from_time.to_time if from_time.respond_to?(:to_time)
|
11
|
+
to_time = to_time.to_time if to_time.respond_to?(:to_time)
|
12
|
+
|
13
|
+
# make sure distance is positive
|
14
|
+
from_time, to_time = to_time, from_time if from_time > to_time
|
15
|
+
|
5
16
|
dist = (to_time - from_time).round
|
6
17
|
|
7
18
|
hours = dist / 3600
|
@@ -10,22 +21,22 @@ module PreciseDistanceOfTimeInWords
|
|
10
21
|
|
11
22
|
words = ''
|
12
23
|
|
13
|
-
|
14
|
-
|
15
|
-
|
24
|
+
I18n.with_options( locale: options[:locale], scope: options[:scope] ) do |locale|
|
25
|
+
words << locale.t(:x_hours, count: hours) if hours > 0
|
26
|
+
words << " #{locale.t(:and)} " if (hours > 0 && minutes > 0) # add_and_between?
|
27
|
+
words << locale.t(:x_minutes, count: minutes) if minutes > 0
|
16
28
|
|
17
|
-
|
18
|
-
|
29
|
+
if seconds > 0 && (minutes <= 0 && hours <= 0)
|
30
|
+
words << locale.t(:x_seconds, count: seconds)
|
31
|
+
end
|
19
32
|
end
|
20
33
|
|
21
34
|
return words
|
22
35
|
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def add_and_between?(words, time_1, time_2)
|
27
|
-
words << ' and ' if (time_1 > 0 && time_2 > 0)
|
28
|
-
end
|
29
36
|
end
|
30
37
|
|
31
|
-
include PreciseDistanceOfTimeInWords
|
38
|
+
include PreciseDistanceOfTimeInWords
|
39
|
+
|
40
|
+
Dir["#{File.dirname(__FILE__)}/precise_distance_of_time_in_words/locales/*.yml"].each do |file|
|
41
|
+
I18n.load_path << file
|
42
|
+
end
|
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
22
|
spec.add_development_dependency 'rake'
|
23
23
|
spec.add_development_dependency 'rspec'
|
24
|
-
spec.add_development_dependency '
|
24
|
+
spec.add_development_dependency 'activesupport'
|
25
25
|
end
|
@@ -12,6 +12,7 @@ describe PreciseDistanceOfTimeInWords do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'calculates time differences properly' do
|
15
|
+
precise_distance_of_time_in_words(1.second).should == '1 second'
|
15
16
|
precise_distance_of_time_in_words(@time1, @time1).should == ''
|
16
17
|
precise_distance_of_time_in_words(@time1, @time2).should == '1 second'
|
17
18
|
precise_distance_of_time_in_words(@time1, @time3).should == '1 minute'
|
@@ -19,5 +20,9 @@ describe PreciseDistanceOfTimeInWords do
|
|
19
20
|
precise_distance_of_time_in_words(@time1, @time5).should == '1 hour and 5 minutes'
|
20
21
|
precise_distance_of_time_in_words(@time1, @time6).should == '1 hour and 5 minutes'
|
21
22
|
end
|
23
|
+
|
24
|
+
it 'support other languages' do
|
25
|
+
precise_distance_of_time_in_words(2.second, 0, locale: :de).should == '2 Sekunden'
|
26
|
+
end
|
22
27
|
end
|
23
28
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: precise_distance_of_time_in_words
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Henrique Passalini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: activesupport
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - '>='
|
@@ -79,6 +79,8 @@ files:
|
|
79
79
|
- README.md
|
80
80
|
- Rakefile
|
81
81
|
- lib/precise_distance_of_time_in_words.rb
|
82
|
+
- lib/precise_distance_of_time_in_words/locales/de.yml
|
83
|
+
- lib/precise_distance_of_time_in_words/locales/en.yml
|
82
84
|
- lib/precise_distance_of_time_in_words/version.rb
|
83
85
|
- precise_distance_of_time_in_words.gemspec
|
84
86
|
- spec/precise_distance_of_time_in_words_spec.rb
|
@@ -103,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
105
|
version: '0'
|
104
106
|
requirements: []
|
105
107
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.0.
|
108
|
+
rubygems_version: 2.0.14
|
107
109
|
signing_key:
|
108
110
|
specification_version: 4
|
109
111
|
summary: A more precise "distance_of_time_in_words"
|