Class: Montrose::Clock

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Clock

Returns a new instance of Clock.



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

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



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

def peek
  return @start_time if @time.nil?

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

    (min_next = times.select { |t| t > @time }.min) && (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



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

def tick
  @time = peek
end