Class: CountDownObj

Inherits:
Object
  • Object
show all
Defined in:
lib/countdownobj.rb,
lib/countdownobj/version.rb

Overview

CountDownObj - Count Down Object by Daniel P. Clark The MIT License

Constant Summary collapse

VERSION =
"0.0.3"
@@second =
1
@@minute =
@@second * 60
@@hour =
@@minute * 60
@@day =
@@hour   * 24
@@week =
@@day    * 7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(countdownto = (Time.now + @@day)) ⇒ CountDownObj

Returns a new instance of CountDownObj.



21
22
23
# File 'lib/countdownobj.rb', line 21

def initialize(countdownto = (Time.now + @@day))
  self.countdownto = countdownto    
end

Instance Attribute Details

#countdowntoObject

Returns the value of attribute countdownto.



19
20
21
# File 'lib/countdownobj.rb', line 19

def countdownto
  @countdownto
end

#dayObject (readonly)

Returns the value of attribute day.



6
7
8
# File 'lib/countdownobj.rb', line 6

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour.



6
7
8
# File 'lib/countdownobj.rb', line 6

def hour
  @hour
end

#minuteObject (readonly)

Returns the value of attribute minute.



6
7
8
# File 'lib/countdownobj.rb', line 6

def minute
  @minute
end

#secondObject (readonly)

Returns the value of attribute second.



6
7
8
# File 'lib/countdownobj.rb', line 6

def second
  @second
end

#weekObject (readonly)

Returns the value of attribute week.



6
7
8
# File 'lib/countdownobj.rb', line 6

def week
  @week
end

Instance Method Details

#deadlineObject



25
26
27
# File 'lib/countdownobj.rb', line 25

def deadline
  self.countdownto - Time.at(Time.now.to_f)
end

#deadline=(timeObject) ⇒ Object



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

def deadline=(timeObject)
  self.countdownto = Time.at(timeObject)
end

#to_sObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/countdownobj.rb', line 29

def to_s
  out = ""
  val = []
  [[@@week, "week"], [@@day, "day"], [@@hour, "hour"], [@@minute, "minute"], [@@second, "second"]].each do |tm|
    if val.empty?
      val = deadline.divmod(tm[0])
    else
      val = val[1].divmod(tm[0])
    end
    if val[0] > 0
      out << "#{val[0]} "
      if val[0] > 1
        out << "#{tm[1]}s"
      else
        out << "#{tm[1]}"
      end
      if not tm[1] == "second"
        out << ", "
      end
    end
  end
  if deadline.to_i <= 0
    out = 'deadline'
  else
    out.chomp(", ")
  end
end