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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99dd828ec9a6f3f8f69ea60589d4b4953fafa992
4
- data.tar.gz: ae89385c82b3ca4f3187781f0ad46b6c795b9b1a
3
+ metadata.gz: 9d25a687e5063796fa37d0dfd752d0c9457ecc2e
4
+ data.tar.gz: 2b80e575d14c0f54d6da86d03115f154d399a672
5
5
  SHA512:
6
- metadata.gz: c5b2cae2c84abfd3f546d21958613095efbbcee2cf3827fef7b66892777188f63fd7655df30302e1441927e1c5614433f3e9fab0422fcc6794ce26835448bc3a
7
- data.tar.gz: ddb4af9acac237e2148748d56fdecfb3c6bf0e56b51aa90d6a41e120417c475495053cce5865611fca63495433d64b7c8b4a863f4dfcaf0884501d8a39a2a420
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
- gem 'precise_distance_of_time_in_words'
10
+ ``` ruby
11
+ gem 'precise_distance_of_time_in_words'
12
+ ```
10
13
 
11
14
  And then execute:
12
15
 
13
- $ bundle
16
+ ``` shell
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it standalone:
14
21
 
15
- Or install it yourself as:
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
- This gem offers to you a method called "precise_distance_of_time_in_words(from, to)"
22
- To you user it just puts that method in where you want (console, view, model) with the times.
23
- For exemple:
24
- precise_distance_of_time_in_words(0, 1) #return '1 second'
25
- precise_distance_of_time_in_words(Time.now, Time.now + 1.hour + 1.minute) #return '1 hour and 1 minute'
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
- words << "#{hours} #{hours > 1 ? 'hours' : 'hour' }" if hours > 0
14
- add_and_between?(words, hours, minutes)
15
- words << "#{minutes} #{minutes > 1 ? 'minutes' : 'minute' }" if minutes > 0
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
- if seconds > 0 && (minutes <= 0 && hours <= 0)
18
- words << "#{seconds} #{seconds > 1 ? 'seconds' : 'second' }"
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
@@ -0,0 +1,13 @@
1
+ de:
2
+ datetime:
3
+ precise_distance_in_words:
4
+ x_minutes:
5
+ one: eine Minute
6
+ other: '%{count} Minuten'
7
+ x_seconds:
8
+ one: eine Sekunde
9
+ other: '%{count} Sekunden'
10
+ x_hours:
11
+ one: eine Stunde
12
+ other: '%{count} Stunden'
13
+ and: und
@@ -0,0 +1,13 @@
1
+ en:
2
+ datetime:
3
+ precise_distance_in_words:
4
+ x_minutes:
5
+ one: '%{count} minute'
6
+ other: '%{count} minutes'
7
+ x_seconds:
8
+ one: '%{count} second'
9
+ other: '%{count} seconds'
10
+ x_hours:
11
+ one: '%{count} hour'
12
+ other: '%{count} hours'
13
+ and: and
@@ -1,3 +1,3 @@
1
1
  module PreciseDistanceOfTimeInWords
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  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 'active_support'
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
@@ -1,5 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'active_support/core_ext/numeric/time'
4
+ require 'active_support/core_ext/object/with_options.rb'
5
+ I18n.locale = :en
4
6
 
5
7
  require 'precise_distance_of_time_in_words.rb'
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.2
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: 2013-08-26 00:00:00.000000000 Z
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: active_support
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.3
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"