meaningful_time 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in meaningful_time.gemspec
4
+ gemspec
5
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,50 @@
1
+ require "meaningful_time/version"
2
+
3
+ class Time
4
+
5
+ def to_meaningful
6
+ diff = (Time.new - self).floor
7
+ if diff == 1
8
+ "#{diff} second ago"
9
+ elsif diff < 60
10
+ "#{diff} seconds ago"
11
+ else
12
+ minutes_passed = diff/60
13
+ if minutes_passed == 1
14
+ "#{minutes_passed} minute ago"
15
+ elsif minutes_passed < 60
16
+ "#{minutes_passed} minutes ago"
17
+ else
18
+ hours_passed = diff/60/60
19
+ if hours_passed == 1
20
+ "#{hours_passed} hour ago"
21
+ elsif hours_passed < 24
22
+ "#{hours_passed} hours ago"
23
+ else
24
+ days_passed = diff/60/60/24
25
+ if days_passed == 1
26
+ "#{days_passed} day ago"
27
+ elsif days_passed < 30
28
+ "#{days_passed} days ago"
29
+ else
30
+ months_passed = diff/60/60/24/30
31
+ if months_passed == 1
32
+ "#{months_passed} month ago"
33
+ elsif months_passed < 12
34
+ "#{months_passed} months ago"
35
+ else
36
+ years_passed = diff/60/60/24/30/12
37
+ if years_passed == 1
38
+ "#{years_passed} year ago"
39
+ elsif years_passed < 6
40
+ "#{years_passed} years ago"
41
+ else
42
+ "a long time ago"
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module MeaningfulTime
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "meaningful_time/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "meaningful_time"
7
+ s.version = MeaningfulTime::VERSION
8
+ s.authors = ["Dennis Suratna"]
9
+ s.email = ["dennis.suratna@cal.berkeley.edu"]
10
+ s.homepage = ""
11
+ s.summary = %q{Adds a convenient method to Time class to print out how long ago a Time instance is passed.}
12
+ s.description = %q{
13
+ # Assuming that today is 1/27/2012
14
+ now = Time.new(2012,1,26)
15
+ puts now.to_meaningful # "1 day ago"
16
+ }
17
+
18
+ s.rubyforge_project = "meaningful_time"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+
25
+ s.add_development_dependency "rspec"
26
+ # specify any dependencies here; for example:
27
+ # s.add_development_dependency "rspec"
28
+ # s.add_runtime_dependency "rest-client"
29
+ end
@@ -0,0 +1,113 @@
1
+ require File.expand_path('../../lib/meaningful_time.rb' ,__FILE__)
2
+ describe Time do
3
+ describe "#to_meaningful" do
4
+
5
+
6
+
7
+ it "works" do
8
+ Time.new.to_meaningful
9
+ end
10
+
11
+ context "before 1 minute" do
12
+ before do
13
+ @past = Time.new(2012, 1,1,10,10,15)
14
+ @now = Time.new(2012, 1,1,10,10,20)
15
+ Time.stub!(:new).and_return(@now)
16
+ end
17
+
18
+ it "returns in seconds" do
19
+ @past.to_meaningful.should == "5 seconds ago"
20
+ end
21
+ end
22
+
23
+ context "before 1 hour" do
24
+ before do
25
+ @past = Time.new(2012, 1,1,10,10,15)
26
+ @now = Time.new(2012, 1,1,10,15,20)
27
+ @one_minute_ago = Time.new(2012, 1,1,10,14,15)
28
+ Time.stub!(:new).and_return(@now)
29
+ end
30
+
31
+ it "returns in minutes" do
32
+ @past.to_meaningful.should == "5 minutes ago"
33
+ end
34
+
35
+ it "returns 1 minute ago" do
36
+ @one_minute_ago.to_meaningful.should == "1 minute ago"
37
+ end
38
+ end
39
+
40
+ context "before 1 day" do
41
+ before do
42
+ @past = Time.new(2012, 1, 1, 7, 10, 15)
43
+ @one_hour_ago = Time.new(2012, 1, 1, 9, 15, 20)
44
+ @now = Time.new(2012, 1,1, 10, 15, 20)
45
+ Time.stub!(:new).and_return(@now)
46
+ end
47
+
48
+ it "returns in minutes" do
49
+ @past.to_meaningful.should == "3 hours ago"
50
+ end
51
+
52
+ it "returns 1 hour ago" do
53
+ @one_hour_ago.to_meaningful.should == "1 hour ago"
54
+ end
55
+ end
56
+
57
+ context "before 1 month" do
58
+ before do
59
+ @past = Time.new(2011, 12,29, 10, 15, 19)
60
+ @one_day_ago = Time.new(2011, 12,31, 10, 15, 19)
61
+ @now = Time.new(2012, 1,1, 10, 15, 20)
62
+ Time.stub!(:new).and_return(@now)
63
+ end
64
+
65
+ it "returns in minutes" do
66
+ @past.to_meaningful.should == "3 days ago"
67
+ end
68
+
69
+ it "returns 1 day ago" do
70
+ @one_day_ago.to_meaningful.should == "1 day ago"
71
+ end
72
+ end
73
+
74
+ context "before 1 year" do
75
+ before do
76
+ @past = Time.new(2011, 10,1, 10, 15, 20)
77
+ @one_month_ago = Time.new(2011, 12,1, 10, 15, 20)
78
+ @now = Time.new(2012, 1,1, 10, 15, 20)
79
+ Time.stub!(:new).and_return(@now)
80
+ end
81
+
82
+ it "returns in months" do
83
+ @past.to_meaningful.should == "3 months ago"
84
+ end
85
+
86
+ it "returns 1 month ago" do
87
+ @one_month_ago.to_meaningful.should == "1 month ago"
88
+ end
89
+ end
90
+
91
+ context "before 5 years" do
92
+ before do
93
+ @past = Time.new(2009, 1,1, 10, 15, 20)
94
+ @long_time = Time.new(2001,1,1,1,1,1);
95
+ @one_year_ago = Time.new(2010, 12,12, 10, 15, 20)
96
+ @now = Time.new(2012, 1,1, 10, 15, 20)
97
+ Time.stub!(:new).and_return(@now)
98
+ end
99
+
100
+ it "returns in years" do
101
+ @past.to_meaningful.should == "3 years ago"
102
+ end
103
+
104
+ it "returns 1 year ago" do
105
+ @one_year_ago.to_meaningful.should == "1 year ago"
106
+ end
107
+
108
+ it "returns long time ago" do
109
+ @long_time.to_meaningful.should == "a long time ago"
110
+ end
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meaningful_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dennis Suratna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2151802100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2151802100
25
+ description: ! "\n # Assuming that today is 1/27/2012\n now = Time.new(2012,1,26)\n
26
+ \ puts now.to_meaningful # \"1 day ago\"\n "
27
+ email:
28
+ - dennis.suratna@cal.berkeley.edu
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - Rakefile
36
+ - lib/meaningful_time.rb
37
+ - lib/meaningful_time/version.rb
38
+ - meaningful_time.gemspec
39
+ - spec/meaningful_time_spec.rb
40
+ homepage: ''
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project: meaningful_time
60
+ rubygems_version: 1.8.10
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Adds a convenient method to Time class to print out how long ago a Time instance
64
+ is passed.
65
+ test_files:
66
+ - spec/meaningful_time_spec.rb