Class: Vpim::Duration
- Inherits:
-
Object
- Object
- Vpim::Duration
- Defined in:
- lib/vpim/duration.rb
Constant Summary collapse
- SECS_HOUR =
60 * 60
- SECS_DAY =
24 * SECS_HOUR
- MINS_HOUR =
60
Class Method Summary collapse
- .as_str(arr) ⇒ Object
- .days(days) ⇒ Object
- .hours(hours) ⇒ Object
- .mins(mins) ⇒ Object
- .secs(secs) ⇒ Object
Instance Method Summary collapse
- #by_days ⇒ Object
- #by_hours ⇒ Object
- #days ⇒ Object
- #hours ⇒ Object
-
#initialize(secs) ⇒ Duration
constructor
Initialize from a number of seconds.
- #mins ⇒ Object
- #secs ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #weeks ⇒ Object
Constructor Details
#initialize(secs) ⇒ Duration
Initialize from a number of seconds.
17 18 19 |
# File 'lib/vpim/duration.rb', line 17 def initialize(secs) @secs = secs end |
Class Method Details
.as_str(arr) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vpim/duration.rb', line 73 def Duration.as_str(arr) s = "" case arr.length when 4 if arr[0] > 0 s << "#{arr[0]} days" end if arr[1] > 0 if s.length > 0 s << ', ' end s << "#{arr[1]} hours" end if arr[2] > 0 if s.length > 0 s << ', ' end s << "#{arr[2]} mins" end if arr[3] > 0 if s.length > 0 s << ', ' end s << "#{arr[3]} secs" end when 3 if arr[0] > 0 s << "#{arr[0]} hours" end if arr[1] > 0 if s.length > 0 s << ', ' end s << "#{arr[1]} mins" end if arr[2] > 0 if s.length > 0 s << ', ' end s << "#{arr[2]} secs" end end s end |
.days(days) ⇒ Object
33 34 35 |
# File 'lib/vpim/duration.rb', line 33 def Duration.days(days) Duration.new(days * SECS_DAY) end |
.hours(hours) ⇒ Object
29 30 31 |
# File 'lib/vpim/duration.rb', line 29 def Duration.hours(hours) Duration.new(hours * SECS_HOUR) end |
Instance Method Details
#by_days ⇒ Object
61 62 63 |
# File 'lib/vpim/duration.rb', line 61 def by_days [ days, hours % 24, mins % MINS_HOUR, secs % 60] end |
#by_hours ⇒ Object
57 58 59 |
# File 'lib/vpim/duration.rb', line 57 def by_hours [ hours, mins % MINS_HOUR, secs % 60] end |
#days ⇒ Object
49 50 51 |
# File 'lib/vpim/duration.rb', line 49 def days (@secs/SECS_DAY).to_i end |
#hours ⇒ Object
45 46 47 |
# File 'lib/vpim/duration.rb', line 45 def hours (@secs/SECS_HOUR).to_i end |
#mins ⇒ Object
41 42 43 |
# File 'lib/vpim/duration.rb', line 41 def mins (@secs/60).to_i end |
#secs ⇒ Object
37 38 39 |
# File 'lib/vpim/duration.rb', line 37 def secs @secs end |
#to_a ⇒ Object
65 66 67 |
# File 'lib/vpim/duration.rb', line 65 def to_a by_days end |
#to_s ⇒ Object
69 70 71 |
# File 'lib/vpim/duration.rb', line 69 def to_s Duration.as_str(self.to_a) end |
#weeks ⇒ Object
53 54 55 |
# File 'lib/vpim/duration.rb', line 53 def weeks (days/7).to_i end |