Class: Unichron
- Inherits:
-
Object
- Object
- Unichron
- Defined in:
- lib/unichron.rb
Instance Attribute Summary collapse
-
#r ⇒ Object
readonly
Returns the value of attribute r.
Instance Method Summary collapse
-
#elapsed(limit = :day) ⇒ Object
Returns the elapsed time in a humanized format e.g.
-
#initialize(obj, mode = nil, debug: false) ⇒ Unichron
constructor
A new instance of Unichron.
- #to_date ⇒ Object
- #to_datetime ⇒ Object
- #to_i ⇒ Object
- #to_seconds ⇒ Object
- #to_time ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(obj, mode = nil, debug: false) ⇒ Unichron
Returns a new instance of Unichron.
14 15 16 17 18 19 |
# File 'lib/unichron.rb', line 14 def initialize(obj, mode=nil, debug: false) @mode, @debug = mode, debug @r = select_method(obj) end |
Instance Attribute Details
#r ⇒ Object (readonly)
Returns the value of attribute r.
12 13 14 |
# File 'lib/unichron.rb', line 12 def r @r end |
Instance Method Details
#elapsed(limit = :day) ⇒ Object
Returns the elapsed time in a humanized format e.g. elapsed(Time.now - 4000) #=> 1h note: An elapsed time more than a day is returned as a calendar date
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/unichron.rb', line 25 def elapsed(limit=:day) elapsed = Time.now - @r relative_time = if elapsed < Time.now - (Date.today - 1).to_time then Subunit.seconds(elapsed).strfunit("%s") else @r.strftime("%b %d") end end |
#to_date ⇒ Object
41 42 43 |
# File 'lib/unichron.rb', line 41 def to_date @r.to_date if valid? end |
#to_datetime ⇒ Object
45 46 47 |
# File 'lib/unichron.rb', line 45 def to_datetime @r.to_datetime if valid? end |
#to_i ⇒ Object
49 50 51 |
# File 'lib/unichron.rb', line 49 def to_i() @r.to_time.to_i if valid? end |
#to_seconds ⇒ Object
37 38 39 |
# File 'lib/unichron.rb', line 37 def to_seconds @r end |
#to_time ⇒ Object
53 54 55 |
# File 'lib/unichron.rb', line 53 def to_time @r.to_time if valid? end |
#valid? ⇒ Boolean
57 58 59 |
# File 'lib/unichron.rb', line 57 def valid? @r.respond_to? :to_time end |