Class: AtRandom::PickTime

Inherits:
Object
  • Object
show all
Defined in:
lib/at-random/pick_time.rb

Defined Under Namespace

Classes: ImpossibleRange

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PickTime

Returns a new instance of PickTime.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/at-random/pick_time.rb', line 5

def initialize(opts={})
  from_h, from_m = parse_time(opts[:from] || '00:00')
  to_h, to_m = parse_time(opts[:to] || '23:59')
  now_h, now_m = parse_time(Time.now.strftime('%H:%M'))

  if now_h >= from_h
    from_h = now_h
    if now_m > from_m
      from_m = now_m
    end
  end

  if now_h > to_h || (now_h == to_h && now_m > to_m)
    raise ImpossibleRange, 'Asked for :to sooner than now'
  end

  @hour = rand(to_h - from_h) + from_h
  @minute = rand(to_m - from_m) + from_m
end

Instance Method Details

#time_sObject



25
26
27
# File 'lib/at-random/pick_time.rb', line 25

def time_s
  format("%02d:%02d", @hour, @minute)
end