taw 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/Gemfile.lock +1 -1
- data/README.md +9 -1
- data/lib/taw.rb +20 -11
- data/lib/taw/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 364a15751f2a8c3b37feea682f21de9d9c04294eb34d85ac107b50dfc7dde66c
|
4
|
+
data.tar.gz: 664902eedba625517ef65244b895ecf40ca5bb80373a04fb69a6bb28f483c742
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a32918bd5062bcfb3611c8478e929bbc436064507f54ea9ed6248218f1a3a2253ab41a54dd13c640adc40e3aa95fb0309f42d5e1c8ce918018276443c618132
|
7
|
+
data.tar.gz: 2a050684f681d2edc17a0e57b7cd075fe71182bcb879cb1e59faa5bde437e0d6c974f37bf854cd224190a061823128aa674f14042ad09766f58953982aefd737
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# Taw
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/shime/taw.svg?branch=master)](https://travis-ci.org/shime/taw)
|
4
|
+
|
3
5
|
**T**ime **A**go in **W**ords helper for non Rails projects.
|
4
|
-
No dependencies. No assumptions about your codebase.
|
6
|
+
No dependencies. No assumptions about your codebase. Minimalistic, ~80 SLOC.
|
5
7
|
|
6
8
|
## Usage
|
7
9
|
|
@@ -16,6 +18,12 @@ Taw.time_ago_in_words(Time.now - 63) + " ago"
|
|
16
18
|
|
17
19
|
Taw.time_ago_in_words(Time.now - 60 * 60 * 2 - 63) + " ago"
|
18
20
|
# => "2 hours and 1 minute and 3 seconds ago"
|
21
|
+
|
22
|
+
Taw.time_ago_in_words(Time.now - 60 * 60 * 26 - 63, approx: 1) + " ago"
|
23
|
+
# => "1 day ago"
|
24
|
+
|
25
|
+
Taw.time_ago_in_words(Time.now - 60 * 60 * 26 - 63, approx: 2) + " ago"
|
26
|
+
# => "1 day and 2 hours ago"
|
19
27
|
```
|
20
28
|
|
21
29
|
## Installation
|
data/lib/taw.rb
CHANGED
@@ -8,11 +8,15 @@ module Taw
|
|
8
8
|
MONTH_IN_SECONDS = DAY_IN_SECONDS * 30
|
9
9
|
YEAR_IN_SECONDS = MONTH_IN_SECONDS * 12
|
10
10
|
|
11
|
-
def self.time_ago_in_words(time)
|
12
|
-
Calculator.new.time_ago_in_words(time)
|
11
|
+
def self.time_ago_in_words(time, opts = {})
|
12
|
+
Calculator.new(opts).time_ago_in_words(time)
|
13
13
|
end
|
14
14
|
|
15
15
|
class Calculator
|
16
|
+
def initialize(opts)
|
17
|
+
@approx_units = opts[:approx]
|
18
|
+
end
|
19
|
+
|
16
20
|
def time_ago_in_words(time)
|
17
21
|
self.distance = Time.now - time
|
18
22
|
calculate_distance
|
@@ -30,6 +34,11 @@ module Taw
|
|
30
34
|
:months,
|
31
35
|
:years
|
32
36
|
|
37
|
+
def approx_units
|
38
|
+
return @approx_units - 1 if @approx_units
|
39
|
+
-1
|
40
|
+
end
|
41
|
+
|
33
42
|
def calculate_distance
|
34
43
|
while distance > 0
|
35
44
|
if distance < MINUTE_IN_SECONDS
|
@@ -80,17 +89,17 @@ module Taw
|
|
80
89
|
|
81
90
|
def output_distance
|
82
91
|
[
|
83
|
-
("#{years} #{pluralize(years, "year"
|
84
|
-
("#{months} #{pluralize(months, "month"
|
85
|
-
("#{weeks} #{pluralize(weeks, "week"
|
86
|
-
("#{days} #{pluralize(days, "day"
|
87
|
-
("#{hours} #{pluralize(hours, "hour"
|
88
|
-
("#{minutes} #{pluralize(minutes, "minute"
|
89
|
-
("#{seconds} #{pluralize(seconds, "second"
|
90
|
-
].compact.join(" and ")
|
92
|
+
("#{years} #{pluralize(years, "year")}" if years && years > 0),
|
93
|
+
("#{months} #{pluralize(months, "month")}" if months && months > 0),
|
94
|
+
("#{weeks} #{pluralize(weeks, "week")}" if weeks && weeks > 0),
|
95
|
+
("#{days} #{pluralize(days, "day")}" if days && days > 0),
|
96
|
+
("#{hours} #{pluralize(hours, "hour")}" if hours && hours > 0),
|
97
|
+
("#{minutes} #{pluralize(minutes, "minute")}" if minutes && minutes > 0),
|
98
|
+
("#{seconds} #{pluralize(seconds, "second")}" if seconds && seconds > 0)
|
99
|
+
].drop_while(&:nil?)[0..approx_units].compact.join(" and ")
|
91
100
|
end
|
92
101
|
|
93
|
-
def pluralize(count, singular, plural)
|
102
|
+
def pluralize(count, singular, plural = "#{singular}s")
|
94
103
|
count > 1 ? plural : singular
|
95
104
|
end
|
96
105
|
end
|
data/lib/taw/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: taw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hrvoje Šimić
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.7.
|
109
|
+
rubygems_version: 2.7.6
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Time ago in words helper for non Rails projects.
|