Class: Cronex::Description

Inherits:
Object
  • Object
show all
Defined in:
lib/cronex/description/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, options = {}) ⇒ Description

Returns a new instance of Description.



6
7
8
9
# File 'lib/cronex/description/base.rb', line 6

def initialize(resources, options = {})
  @resources = resources
  @options = options || {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/cronex/description/base.rb', line 4

def options
  @options
end

#resourcesObject

Returns the value of attribute resources.



4
5
6
# File 'lib/cronex/description/base.rb', line 4

def resources
  @resources
end

Instance Method Details

#plural(expression, singular, plural) ⇒ Object



52
53
54
55
56
57
# File 'lib/cronex/description/base.rb', line 52

def plural(expression, singular, plural)
  number = Cronex::Utils.number?(expression)
  return plural if number && number > 1
  return plural if expression.include?(',')
  singular
end

#segment_description(expression, all_description) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cronex/description/base.rb', line 15

def segment_description(expression, all_description)
  if expression.empty? || expression == '0'
    desc = ''
  elsif expression == '*'
    desc = all_description
  elsif !Cronex::Utils.include_any?(expression, special_chars)
    desc = format(description_format(expression), single_item_description(expression))
  elsif expression.include?('/')
    segments = expression.split('/')
    desc = format(interval_description_format(segments[1]), single_item_description(segments[1]))
    # interval contains 'between' piece (e.g. 2-59/3)
    if segments[0].include?('-')
      between_segment_of_interval = segments[0]
      between_segments = between_segment_of_interval.split('-')
      desc += ', ' + format(
        between_description_format(between_segment_of_interval),
        single_item_description(between_segments[0]),
        single_item_description(between_segments[1]).gsub(':00', ':59'))
    elsif !Cronex::Utils.include_any?(segments[0], special_chars + ['*'])
      desc += ', ' + format(starting_description_format(segments[0]), single_item_description(segments[0]))
    end
  elsif expression.include?('-')
    segments = expression.split('-')
    desc = format(
      between_description_format(expression),
      single_item_description(segments[0]),
      single_item_description(segments[1]).gsub(':00', ':59'))
  elsif expression.include?(',')
    segments = expression.split(',')
    segments = segments.map { |s| single_item_description(s) }
    desc_content = segments[0...-1].join(', ') + ' ' + resources.get('and') + ' ' + segments.last
    desc = format(description_format(expression), desc_content)
  end

  desc
end

#special_charsObject



11
12
13
# File 'lib/cronex/description/base.rb', line 11

def special_chars
  ['/', '-', ',']
end