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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3b9a19c55e5617db5db2515ac68aea8ee0b11d79dfc7020c5acb5238e388166
4
- data.tar.gz: 52b4fe9f40ee42375290ac0c482f93aa54616d8e261a427fab5881d522bb1381
3
+ metadata.gz: 364a15751f2a8c3b37feea682f21de9d9c04294eb34d85ac107b50dfc7dde66c
4
+ data.tar.gz: 664902eedba625517ef65244b895ecf40ca5bb80373a04fb69a6bb28f483c742
5
5
  SHA512:
6
- metadata.gz: ac42d4fca0c8b983350c9d86fb8c15875dcb49218f7fa72eaacc66c64f057a7aeed39f0d879a57f29b2e083c9c8fae98db5a858e0991d2969f7f59e12ee52c37
7
- data.tar.gz: 0e37234d8a401c648cbd3fecc5a2666869389f98eb5566ef4ddb5dad60194b1546774bd1a30e808cbee60679bea9308c2c39f101b4f66e214de4f9c4cd556465
6
+ metadata.gz: 7a32918bd5062bcfb3611c8478e929bbc436064507f54ea9ed6248218f1a3a2253ab41a54dd13c640adc40e3aa95fb0309f42d5e1c8ce918018276443c618132
7
+ data.tar.gz: 2a050684f681d2edc17a0e57b7cd075fe71182bcb879cb1e59faa5bde437e0d6c974f37bf854cd224190a061823128aa674f14042ad09766f58953982aefd737
@@ -2,4 +2,6 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.5.0
5
- before_install: gem install bundler -v 1.16.1
5
+ - 2.4.3
6
+ - 2.3.6
7
+ install: bundle install --jobs=3 --retry=3
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- taw (1.0.0pre)
4
+ taw (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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", "years")}" if years && years > 0),
84
- ("#{months} #{pluralize(months, "month", "months")}" if months && months > 0),
85
- ("#{weeks} #{pluralize(weeks, "week", "weeks")}" if weeks && weeks > 0),
86
- ("#{days} #{pluralize(days, "day", "days")}" if days && days > 0),
87
- ("#{hours} #{pluralize(hours, "hour", "hours")}" if hours && hours > 0),
88
- ("#{minutes} #{pluralize(minutes, "minute", "minutes")}" if minutes && minutes > 0),
89
- ("#{seconds} #{pluralize(seconds, "second", "seconds")}" if seconds && seconds > 0)
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
@@ -1,3 +1,3 @@
1
1
  module Taw
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
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.0.0
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-01-28 00:00:00.000000000 Z
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.3
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.