human-date 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,6 +3,7 @@ human-date
3
3
 
4
4
  Installation:
5
5
  -------------
6
+
6
7
  ```
7
8
  gem install human-date
8
9
  ```
@@ -11,18 +12,19 @@ Usage:
11
12
  ------
12
13
 
13
14
  ```ruby
14
- date_translator = HumanDate::DateTranslator.new
15
- from = DateTime.now
16
- sleep 10
17
- to = DateTime.now
15
+ require 'human-date'
16
+
17
+ translator = HumanDate::DateTranslator.new
18
+ date1 = DateTime.parse "2011-10-23T00:00:00"
19
+ date2 = DateTime.parse "2012-12-25T02:05:10"
18
20
 
19
- date_translator.translate(from, to)
20
- => '10 seconds'
21
+ translator.translate(date1, date2)
22
+ #=> 1 year, 2 months, 4 days, 2 hours, 5 minutes and 10 seconds
21
23
 
22
- date_translator.translate(to, from)
23
- => '10 seconds ago'
24
+ translator.translate(date2, date1)
25
+ #=> 1 year, 2 months, 4 days, 2 hours, 5 minutes and 10 seconds ago
24
26
 
25
- date_translator.tolerance = 10
26
- date_translator.translate(from, to)
27
- => 'now'
27
+ translator.parts = [:year, :month, :day]
28
+ translator.translate(date1, date2)
29
+ #=> 1 year, 2 months and 4 days
28
30
  ```
data/Rakefile CHANGED
@@ -4,3 +4,10 @@ require 'rake/testtask'
4
4
  Rake::TestTask.new do |t|
5
5
  t.pattern = "test/test_*.rb"
6
6
  end
7
+
8
+ require 'erb'
9
+ task :examples do
10
+ require_relative 'lib/human-date'
11
+ template = ERB.new(File.read('examples/example.erb'))
12
+ puts template.result(binding).gsub /\n{3,}/, "\n"
13
+ end
@@ -0,0 +1,15 @@
1
+ require 'human-date'
2
+
3
+ translator = HumanDate::DateTranslator.new <% translator = HumanDate::DateTranslator.new %>
4
+ date1 = DateTime.parse "2011-10-23T00:00:00" <% date1 = DateTime.parse "2011-10-23T00:00:00" %>
5
+ date2 = DateTime.parse "2012-12-25T02:05:10" <% date2 = DateTime.parse "2012-12-25T02:05:10" %>
6
+
7
+ translator.translate(date1, date2)
8
+ #=> <%= translator.translate(date1, date2) %>
9
+
10
+ translator.translate(date2, date1)
11
+ #=> <%= translator.translate(date2, date1) %>
12
+
13
+ translator.parts = [:year, :month, :day] <% translator.parts = [:year, :month, :day] %>
14
+ translator.translate(date1, date2)
15
+ #=> <%= translator.translate(date1, date2) %>
@@ -18,4 +18,5 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
  s.add_development_dependency "minitest"
21
+ s.add_development_dependency "erubis"
21
22
  end
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  module HumanDate
4
4
  class DateTranslator
5
- attr_accessor :tolerance
5
+ attr_accessor :tolerance, :parts
6
6
 
7
7
  TIME = {
8
8
  :year => 365 * 24 * 60 * 60,
@@ -15,20 +15,43 @@ module HumanDate
15
15
 
16
16
  def initialize
17
17
  @tolerance = 1
18
+ @parts = TIME.keys
18
19
  end
19
20
 
20
- def translate(from, to)
21
+ def translate from, to
21
22
  total_difference_in_seconds = ((to-from) * 60 * 60 * 24).to_i
22
- return 'now' if total_difference_in_seconds.abs <= tolerance
23
-
24
- TIME.keys.each do |key|
25
- if total_difference_in_seconds.abs >= TIME[key]
26
- result = "#{total_difference_in_seconds.abs / TIME[key]} #{key}"
27
- result += 's' if total_difference_in_seconds.abs >= (TIME[key] * 2)
28
- result += ' ago' if total_difference_in_seconds < 0
29
- return result
23
+ absolute_total_difference_in_seconds = total_difference_in_seconds.abs
24
+ return 'now' if absolute_total_difference_in_seconds <= tolerance
25
+
26
+ words = []
27
+ parts.each do |key|
28
+ if absolute_total_difference_in_seconds >= TIME[key]
29
+ count = 0
30
+ while absolute_total_difference_in_seconds >= TIME[key]
31
+ absolute_total_difference_in_seconds -= TIME[key]
32
+ count += 1
33
+ end
34
+ word = "#{count} #{key}"
35
+ word += 's' if count >= 2
36
+ words << word
30
37
  end
31
38
  end
39
+
40
+ sentence = join_words(words)
41
+ sentence += ' ago' if total_difference_in_seconds < 0
42
+ sentence
43
+ end
44
+
45
+ private
46
+
47
+ def join_words(words)
48
+ if words.size > 2
49
+ last_two_words = "#{words[-2]} and #{words[-1]}"
50
+ words.pop
51
+ words.pop
52
+ words.push(last_two_words)
53
+ end
54
+ words.join(", ")
32
55
  end
33
56
 
34
57
  end
@@ -1,5 +1,5 @@
1
1
  module Human
2
2
  module Date
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -25,13 +25,13 @@ class DateTranslatorTest < MiniTest::Unit::TestCase
25
25
  end
26
26
 
27
27
  def test_translate_returns_years
28
- assert_equal "1 year", translate("2012-10-23T00:00:00")
29
- assert_equal "2 years", translate("2013-10-23T00:00:00")
28
+ assert_equal "1 year", translate("2012-10-22T00:00:00")
29
+ assert_equal "2 years", translate("2013-10-22T00:00:00")
30
30
  end
31
31
 
32
32
  def test_translate_returns_months
33
- assert_equal "1 month", translate("2011-11-23T00:00:00")
34
- assert_equal "2 months", translate("2011-12-23T00:00:00")
33
+ assert_equal "1 month", translate("2011-11-22T00:00:00")
34
+ assert_equal "2 months", translate("2011-12-22T00:00:00")
35
35
  end
36
36
 
37
37
  def test_translate_returns_days
@@ -60,4 +60,13 @@ class DateTranslatorTest < MiniTest::Unit::TestCase
60
60
  assert_equal "1 day ago", translate("2011-10-22T00:00:00")
61
61
  end
62
62
 
63
+ def test_translate_returns_all_possible_times
64
+ assert_equal "1 year, 2 months, 4 days, 2 hours, 5 minutes and 10 seconds", translate("2012-12-25T02:05:10")
65
+ end
66
+
67
+ def test_translage_only_returns_specified_values
68
+ @date_translator.parts = [:year, :month, :day]
69
+ assert_equal "1 year, 2 months and 4 days", translate("2012-12-25T00:00:00")
70
+ end
71
+
63
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human-date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
17
- requirement: &70305826377680 !ruby/object:Gem::Requirement
17
+ requirement: &70231184237040 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,18 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70305826377680
25
+ version_requirements: *70231184237040
26
+ - !ruby/object:Gem::Dependency
27
+ name: erubis
28
+ requirement: &70231184236620 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70231184236620
26
37
  description: Human readable date differences
27
38
  email:
28
39
  - andrew.vos@gmail.com
@@ -34,6 +45,7 @@ files:
34
45
  - Gemfile
35
46
  - README.md
36
47
  - Rakefile
48
+ - examples/example.erb
37
49
  - human-date.gemspec
38
50
  - lib/human-date.rb
39
51
  - lib/human-date/date_translator.rb