Class: Montrose::Clock

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Clock


6
7
8
9
10
11
12
13
# File 'lib/montrose/clock.rb', line 6

def initialize(opts = {})
  @options = Montrose::Options.merge(opts)
  @time = nil
  @every = @options.fetch(:every) { fail ConfigurationError, "Required option :every not provided" }
  @interval = @options.fetch(:interval)
  @start_time = @options.fetch(:start_time)
  @at = @options.fetch(:at, nil)
end

Instance Method Details

#peekObject


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/montrose/clock.rb', line 22

def peek
  return @start_time if @time.nil?

  if @at
    times = @at.map { |(hour, min)| @time.change(hour: hour, min: min) }

    min_next = times.select { |t| t > @time }.min and return min_next

    advance_step(times.min || @time)
  else
    advance_step(@time)
  end
end

#tickObject

Advances time to new unit by increment and sets new time as “current” time for next tick


18
19
20
# File 'lib/montrose/clock.rb', line 18

def tick
  @time = peek
end