Class: Unichron

Inherits:
Object
  • Object
show all
Defined in:
lib/unichron.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#rObject (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_dateObject



41
42
43
# File 'lib/unichron.rb', line 41

def to_date
  @r.to_date if valid?
end

#to_datetimeObject



45
46
47
# File 'lib/unichron.rb', line 45

def to_datetime
  @r.to_datetime if valid?
end

#to_iObject



49
50
51
# File 'lib/unichron.rb', line 49

def to_i()
  @r.to_time.to_i if valid?
end

#to_secondsObject



37
38
39
# File 'lib/unichron.rb', line 37

def to_seconds
  @r
end

#to_timeObject



53
54
55
# File 'lib/unichron.rb', line 53

def to_time
  @r.to_time if valid?
end

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/unichron.rb', line 57

def valid?
  @r.respond_to? :to_time
end