Class: Async::Cron::Period

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

Direct Known Subclasses

Hours, Minutes, Month, Monthday, Seconds, Weekday

Constant Summary collapse

RANGE =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, divisor = 1, range: self.class::RANGE) ⇒ Period

Returns a new instance of Period.



41
42
43
44
45
46
47
48
49
50
# File 'lib/async/cron/period.rb', line 41

def initialize(value = nil, divisor = 1, range: self.class::RANGE)
  @value = value
  @divisor = divisor
  @range = range
  
  if @values = divide(expand(@value))
    # This is an optimization to avoid recalculating the successors every time:
    @successors = successors(@values)
  end
end

Class Method Details

.parse(string) ⇒ Object

Parse a string into a period.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/async/cron/period.rb', line 13

def self.parse(string)
  value, divisor = string&.split('/', 2)
  
  if divisor
    divisor = Integer(divisor)
  else
    divisor = 1
  end
  
  if value == '*'
    value = nil
  elsif value
    value = value.split(',').map do |part|
      if part =~ /\A(\d+)-(\d+)\z/
        Range.new(Integer($1), Integer($2))
      elsif part =~ /\A(-?\d+)\.\.(-?\d+)\z/
        Range.new(Integer($1), Integer($2))
      else
        Integer(part)
      end
    end
  end
  
  self.new(value, divisor)
end

Instance Method Details

#include?(time) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/async/cron/period.rb', line 75

def include?(time)
  if @values
    return @values.include?(value_from(time.normalize!))
  else
    return true
  end
end

#increment(time) ⇒ Object

Increment the specific time unit to the next possible value.



58
59
60
# File 'lib/async/cron/period.rb', line 58

def increment(time)
  time.seconds = @successors[time.seconds]
end

#reset(time) ⇒ Object

Reset the specific time unit to the first value.



71
72
73
# File 'lib/async/cron/period.rb', line 71

def reset(time)
  time.seconds = @values.first
end

#step(time) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/async/cron/period.rb', line 62

def step(time)
  time = Time.from(time).normalize!
  
  increment(time)
  
  return time
end

#to_aObject



52
53
54
# File 'lib/async/cron/period.rb', line 52

def to_a
  @values
end