Class: CronUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/khronotab/cron_unit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cron, min, max) ⇒ CronUnit

Returns a new instance of CronUnit.



4
5
6
7
8
# File 'lib/khronotab/cron_unit.rb', line 4

def initialize(cron, min, max)
  @cron_form = cron
  @minimum = min
  @maximum = max
end

Instance Attribute Details

#cron_formObject

Returns the value of attribute cron_form.



2
3
4
# File 'lib/khronotab/cron_unit.rb', line 2

def cron_form
  @cron_form
end

#maximumObject

Returns the value of attribute maximum.



2
3
4
# File 'lib/khronotab/cron_unit.rb', line 2

def maximum
  @maximum
end

#minimumObject

Returns the value of attribute minimum.



2
3
4
# File 'lib/khronotab/cron_unit.rb', line 2

def minimum
  @minimum
end

Instance Method Details

#contains?(value) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/khronotab/cron_unit.rb', line 10

def contains?(value)
  expanded_form.include?(value)
end

#expand_interval(segment) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/khronotab/cron_unit.rb', line 19

def expand_interval(segment)
  counter = 0
  times = []
  interval = segment.split('/').map! { |x|
    if x == '*'
      x = @maximum
    else
      x.to_i
    end
  }
  while counter < interval[0]
    times.push(counter)
    counter += interval[1]
  end
  times
end

#expand_range(segment) ⇒ Object



14
15
16
17
# File 'lib/khronotab/cron_unit.rb', line 14

def expand_range(segment)
  range = segment.split('-').map! { |x| x.to_i }
  (range[0] .. range[1]).to_a
end

#expanded_formObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/khronotab/cron_unit.rb', line 36

def expanded_form
  values = []
  @cron_form.split(',').each do |segment|
    case segment
      when /-/ #range
        values.concat(expand_range(segment))
      when /\// #interval
        values.concat(expand_interval(segment))
      when /[*]/
        values.concat((@minimum .. @maximum).to_a)
      else
        values << segment.to_i
    end
  end
  values.sort.uniq
end

#to_sObject



53
54
55
# File 'lib/khronotab/cron_unit.rb', line 53

def to_s
  @cron_form
end