Module: TimeWindow
- Defined in:
- lib/time_window.rb,
lib/time_window/version.rb
Constant Summary collapse
- InvalidUnit =
Class.new(StandardError)
- InvalidWindow =
Class.new(StandardError)
- ALIASES =
{ "s" => "second", "m" => "minute", "h" => "hour" }.freeze
- VERSION =
"0.1.0"
Class Attribute Summary collapse
-
.now_proc ⇒ Object
Returns the value of attribute now_proc.
Class Method Summary collapse
-
.call(window, time = now) ⇒ Object
Retrieve the formatted date time window.
- .now ⇒ Object
- .process_hour_window(time, window) ⇒ Object
- .process_minute_window(time, window) ⇒ Object
- .process_second_window(time, window) ⇒ Object
Class Attribute Details
.now_proc ⇒ Object
Returns the value of attribute now_proc.
14 15 16 |
# File 'lib/time_window.rb', line 14 def now_proc @now_proc end |
Class Method Details
.call(window, time = now) ⇒ Object
Retrieve the formatted date time window. The ‘time` param defaults to the current time. Returns a `Time` object.
Accept units are: s (second), m (minute), h (hour).
TimeWindow.call("5m")
#=> 2018-12-10 05:40:00 -0800
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/time_window.rb', line 32 def self.call(window, time = now) _, window, unit = *window.match(/\A(\d+)(.)\z/) window = Integer(window) unit = ALIASES.fetch(unit) { raise InvalidUnit, "#{unit.inspect} is not a valid unit" } hour, minute, second = public_send("process_#{unit}_window", time, window) Time.new(time.year, time.month, time.day, hour, minute, second) end |
.now ⇒ Object
19 20 21 |
# File 'lib/time_window.rb', line 19 def self.now now_proc.call end |
.process_hour_window(time, window) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/time_window.rb', line 59 def self.process_hour_window(time, window) raise InvalidWindow, "#{window.inspect} must be covered by 1..23" unless (1..23).cover?(window) hour = time.hour - (time.hour % window) [hour, 0, 0] end |
.process_minute_window(time, window) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/time_window.rb', line 51 def self.process_minute_window(time, window) raise InvalidWindow, "#{window.inspect} must be covered by 1..59" unless (1..59).cover?(window) minute = time.min - (time.min % window) [time.hour, minute, 0] end |
.process_second_window(time, window) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/time_window.rb', line 43 def self.process_second_window(time, window) raise InvalidWindow, "#{window.inspect} must be covered by 1..59" unless (1..59).cover?(window) second = time.sec - (time.sec % window) [time.hour, time.min, second] end |