Class: BBLib::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/time/cron.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp) ⇒ Cron

Returns a new instance of Cron.



6
7
8
9
# File 'lib/time/cron.rb', line 6

def initialize exp
  @parts = Hash.new
  self.exp = exp
end

Instance Attribute Details

#expObject

Returns the value of attribute exp.



4
5
6
# File 'lib/time/cron.rb', line 4

def exp
  @exp
end

#partsObject (readonly)

Returns the value of attribute parts.



4
5
6
# File 'lib/time/cron.rb', line 4

def parts
  @parts
end

#timeObject (readonly)

Returns the value of attribute time.



4
5
6
# File 'lib/time/cron.rb', line 4

def time
  @time
end

Class Method Details

.next(exp, count: 1, time: Time.now) ⇒ Object



33
34
35
# File 'lib/time/cron.rb', line 33

def self.next exp, count: 1, time: Time.now
  t = BBLib::Cron.new(exp).next(count:count, time:time)
end

.prev(exp, count: 1, time: Time.now) ⇒ Object



37
38
39
# File 'lib/time/cron.rb', line 37

def self.prev exp, count: 1, time: Time.now
  BBLib::Cron.new(exp).prev(count:count, time:time)
end

.valid?(exp) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/time/cron.rb', line 41

def self.valid? exp
  !(numeralize(exp) =~ /\A(.*?\s){4,5}.*?\S\z/).nil?
end

Instance Method Details

#closest(exp = @exp, direction: 1, count: 1, time: Time.now) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/time/cron.rb', line 11

def closest exp = @exp, direction:1, count: 1, time: Time.now
  if exp then self.exp = exp end
  results = []
  return results unless @exp
  (1..count).each{ |i| results.push next_time(i == 1 ? time : results.last, direction) }
  count <= 1 ? results.first : results.reject{ |r| r.nil? }
end

#next(exp = @exp, count: 1, time: Time.now) ⇒ Object



19
20
21
# File 'lib/time/cron.rb', line 19

def next exp = @exp, count: 1, time: Time.now
  closest exp, count:count, time:time, direction:1
end

#prev(exp = @exp, count: 1, time: Time.now) ⇒ Object



23
24
25
# File 'lib/time/cron.rb', line 23

def prev exp = @exp, count: 1, time: Time.now
  closest exp, count:count, time:time, direction:-1
end

#valid?(exp) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/time/cron.rb', line 45

def valid? exp
  BBLib::Cron.valid?(exp)
end