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.
16 17 18 |
# File 'lib/vpim/duration.rb', line 16 def initialize(secs) @secs = secs end |
Class Method Details
.as_str(arr) ⇒ Object
72 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 |
# File 'lib/vpim/duration.rb', line 72 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
32 33 34 |
# File 'lib/vpim/duration.rb', line 32 def Duration.days(days) Duration.new(days * SECS_DAY) end |
.hours(hours) ⇒ Object
28 29 30 |
# File 'lib/vpim/duration.rb', line 28 def Duration.hours(hours) Duration.new(hours * SECS_HOUR) end |
Instance Method Details
#by_days ⇒ Object
60 61 62 |
# File 'lib/vpim/duration.rb', line 60 def by_days [ days, hours % 24, mins % MINS_HOUR, secs % 60] end |
#by_hours ⇒ Object
56 57 58 |
# File 'lib/vpim/duration.rb', line 56 def by_hours [ hours, mins % MINS_HOUR, secs % 60] end |
#days ⇒ Object
48 49 50 |
# File 'lib/vpim/duration.rb', line 48 def days (@secs/SECS_DAY).to_i end |
#hours ⇒ Object
44 45 46 |
# File 'lib/vpim/duration.rb', line 44 def hours (@secs/SECS_HOUR).to_i end |
#mins ⇒ Object
40 41 42 |
# File 'lib/vpim/duration.rb', line 40 def mins (@secs/60).to_i end |
#secs ⇒ Object
36 37 38 |
# File 'lib/vpim/duration.rb', line 36 def secs @secs end |
#to_a ⇒ Object
64 65 66 |
# File 'lib/vpim/duration.rb', line 64 def to_a by_days end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/vpim/duration.rb', line 68 def to_s Duration.as_str(self.to_a) end |
#weeks ⇒ Object
52 53 54 |
# File 'lib/vpim/duration.rb', line 52 def weeks (days/7).to_i end |