Class: NaturalTime
- Inherits:
-
Object
- Object
- NaturalTime
- Defined in:
- lib/natural_time.rb,
lib/natural_time/version.rb
Constant Summary collapse
- UNITS_OF_TIME =
[["year", "years"], ["month", "months"], ["week", "weeks"], ["day", "days"], ["hour", "hours"], ["minute", "minutes"]]
- VERSION =
"0.3.0"
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#past ⇒ Object
Returns the value of attribute past.
-
#precision ⇒ Object
Returns the value of attribute precision.
Instance Method Summary collapse
- #distance ⇒ Object
- #elapsed_time(duration_in_seconds) ⇒ Object
-
#initialize(duration_in_seconds, options = {}) ⇒ NaturalTime
constructor
A new instance of NaturalTime.
- #natural_time ⇒ Object
- #to_a ⇒ Object
- #to_array ⇒ Object
- #to_s ⇒ Object
- #to_sentence ⇒ Object
Constructor Details
#initialize(duration_in_seconds, options = {}) ⇒ NaturalTime
Returns a new instance of NaturalTime.
9 10 11 12 13 14 15 |
# File 'lib/natural_time.rb', line 9 def initialize(duration_in_seconds, ={}) @precision = [:precision] duration_in_seconds = duration_in_seconds.to_i @past = duration_in_seconds < 1 @duration = duration_in_seconds.abs elapsed_time = elapsed_time(duration_in_seconds) end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
5 6 7 |
# File 'lib/natural_time.rb', line 5 def duration @duration end |
#past ⇒ Object
Returns the value of attribute past.
5 6 7 |
# File 'lib/natural_time.rb', line 5 def past @past end |
#precision ⇒ Object
Returns the value of attribute precision.
5 6 7 |
# File 'lib/natural_time.rb', line 5 def precision @precision end |
Instance Method Details
#distance ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/natural_time.rb', line 37 def distance if past modifier = "ago" else modifier = "from now" end "#{to_sentence} #{modifier}" end |
#elapsed_time(duration_in_seconds) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/natural_time.rb', line 46 def elapsed_time(duration_in_seconds) return "0 seconds" if duration_in_seconds <= 0 elapsed_time = [] seconds_left = duration_in_seconds UNITS_OF_TIME.each do |period| amount = (seconds_left / 1.send(period.first)).to_i str = amount == 1 ? period[0] : period[1] elapsed_time << "#{amount} #{str}" if amount > 0 seconds_left = seconds_left % 1.send(period.first) end seconds = seconds_left % 1.minute str = seconds == 1 ? "second" : "seconds" elapsed_time << "#{seconds.to_i} #{str}" unless (seconds == 0 && elapsed_time.compact.length > 0) if precision elapsed_time.compact.first(precision) else elapsed_time.compact end end |
#natural_time ⇒ Object
21 22 23 |
# File 'lib/natural_time.rb', line 21 def natural_time to_array.join(", ") end |
#to_a ⇒ Object
29 30 31 |
# File 'lib/natural_time.rb', line 29 def to_a [elapsed_time(duration)].flatten end |
#to_array ⇒ Object
33 34 35 |
# File 'lib/natural_time.rb', line 33 def to_array to_a end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/natural_time.rb', line 25 def to_s natural_time end |
#to_sentence ⇒ Object
17 18 19 |
# File 'lib/natural_time.rb', line 17 def to_sentence to_array.to_sentence end |