Class: Cronex::ExpressionDescriptor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, options = {}, locale = nil) ⇒ ExpressionDescriptor

Returns a new instance of ExpressionDescriptor.



21
22
23
24
25
26
27
# File 'lib/cronex/exp_descriptor.rb', line 21

def initialize(expression, options = {}, locale = nil)
  @expression = expression
  @options = CRONEX_OPTS.merge(options)
  @expression_parts = []
  @parsed = false
  @resources = Cronex::Resource.new(locale)
end

Instance Attribute Details

#expressionObject

Returns the value of attribute expression.



19
20
21
# File 'lib/cronex/exp_descriptor.rb', line 19

def expression
  @expression
end

#expression_partsObject

Returns the value of attribute expression_parts.



19
20
21
# File 'lib/cronex/exp_descriptor.rb', line 19

def expression_parts
  @expression_parts
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/cronex/exp_descriptor.rb', line 19

def options
  @options
end

#parsedObject

Returns the value of attribute parsed.



19
20
21
# File 'lib/cronex/exp_descriptor.rb', line 19

def parsed
  @parsed
end

#resourcesObject

Returns the value of attribute resources.



19
20
21
# File 'lib/cronex/exp_descriptor.rb', line 19

def resources
  @resources
end

Instance Method Details

#day_of_month_description(expression_parts) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cronex/exp_descriptor.rb', line 107

def day_of_month_description(expression_parts)
  exp = expression_parts[3].gsub('?', '*')
  if exp == 'L'
    description = ', ' + resources.get('on_the_last_day_of_the_month')
  elsif exp == 'LW' || exp == 'WL'
    description = ', ' + resources.get('on_the_last_weekday_of_the_month')
  else
    match = exp.match(/(\d{1,2}W)|(W\d{1,2})/)
    if match
      day_num = Cronex::Utils.integer(match[0].gsub('W', ''))
      day_str = day_num == 1 ? resources.get('first_weekday') : format(resources.get('weekday_nearest_day'), day_num)
      description = format(', ' + resources.get('on_the_of_the_month'), day_str)
    else
      desc = DayOfMonthDescription.new(resources)
      description = desc.segment_description(exp, ', ' + resources.get('every_day'))
    end
  end
  description
end

#day_of_week_description(expression_parts) ⇒ Object



97
98
99
100
# File 'lib/cronex/exp_descriptor.rb', line 97

def day_of_week_description(expression_parts)
  desc = DayOfWeekDescription.new(resources, options)
  desc.segment_description(expression_parts[5], ', ' + resources.get('every_day'))
end

#description(type = :full) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cronex/exp_descriptor.rb', line 33

def description(type = :full)
  desc = ''

  begin
    unless parsed
      parser = Parser.new(expression, options)
      @expression_parts = parser.parse
      @parsed = true
    end

    desc = case type
    when :full
      full_description(expression_parts)
    when :timeofday
      time_of_day_description(expression_parts)
    when :hours
      hours_description(expression_parts)
    when :minutes
      minutes_description(expression_parts)
    when :seconds
      seconds_description(expression_parts)
    when :dayofmonth
      day_of_month_description(expression_parts)
    when :month
      month_description(expression_parts)
    when :dayofweek
      day_of_week_description(expression_parts)
    when :year
      year_description(expression_parts)
    else
      seconds_description(expression_parts)
    end
  rescue StandardError => e
    if options[:throw_exception_on_parse_error]
      raise e
    else
      desc = e.message
      puts "Exception parsing expression: #{e}"
    end
  end

  desc
end

#full_description(expression_parts) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cronex/exp_descriptor.rb', line 159

def full_description(expression_parts)
  time_segment = time_of_day_description(expression_parts)
  dom_desc = day_of_month_description(expression_parts)
  month_desc = month_description(expression_parts)
  dow_desc = day_of_week_description(expression_parts)
  year_desc = year_description(expression_parts)
  description = format('%s%s%s%s%s', time_segment, dom_desc, dow_desc, month_desc, year_desc)
  description = transform_verbosity(description, options[:verbose])
  description = transform_case(description, options[:casing])
  description
end

#hours_description(expression_parts) ⇒ Object



87
88
89
90
# File 'lib/cronex/exp_descriptor.rb', line 87

def hours_description(expression_parts)
  desc = HoursDescription.new(resources)
  desc.segment_description(expression_parts[2], resources.get('every_hour'))
end

#minutes_description(expression_parts) ⇒ Object



82
83
84
85
# File 'lib/cronex/exp_descriptor.rb', line 82

def minutes_description(expression_parts)
  desc = MinutesDescription.new(resources)
  desc.segment_description(expression_parts[1], resources.get('every_minute'))
end

#month_description(expression_parts) ⇒ Object



102
103
104
105
# File 'lib/cronex/exp_descriptor.rb', line 102

def month_description(expression_parts)
  desc = MonthDescription.new(resources)
  desc.segment_description(expression_parts[4], '')
end

#seconds_description(expression_parts) ⇒ Object



77
78
79
80
# File 'lib/cronex/exp_descriptor.rb', line 77

def seconds_description(expression_parts)
  desc = SecondsDescription.new(resources)
  desc.segment_description(expression_parts[0], resources.get('every_second'))
end

#time_of_day_description(expression_parts) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cronex/exp_descriptor.rb', line 127

def time_of_day_description(expression_parts)
  sec_exp, min_exp, hour_exp = expression_parts
  description = ''
  if [sec_exp, min_exp, hour_exp].all? { |exp| !Cronex::Utils.include_any?(exp, SPECIAL_CHARS) }
    # specific time of day (i.e. 10 14)
    description += resources.get('at') + ' ' + Cronex::Utils.format_time(hour_exp, min_exp, sec_exp)
  elsif min_exp.include?('-') && !min_exp.include?('/') && !min_exp.include?(',') && !Cronex::Utils.include_any?(hour_exp, SPECIAL_CHARS)
    # Minute range in single hour (e.g. 0-10 11)
    min_parts = min_exp.split('-')
    description += format(
      resources.get('every_minute_between'),
      Cronex::Utils.format_time(hour_exp, min_parts[0]),
      Cronex::Utils.format_time(hour_exp, min_parts[1]))
  elsif hour_exp.include?(',') && !Cronex::Utils.include_any?(min_exp, SPECIAL_CHARS)
    # Hours list with single minute (e.g. 30 6,14,16)
    hour_parts = hour_exp.split(',')
    description += resources.get('at')
    h_parts = hour_parts.map { |part| ' ' + Cronex::Utils.format_time(part, min_exp) }
    description += h_parts[0...-1].join(',') + ' ' + resources.get('and') + h_parts.last
  else
    sec_desc = seconds_description(expression_parts)
    min_desc = minutes_description(expression_parts)
    hour_desc = hours_description(expression_parts)
    description += sec_desc
    description += ', ' if description.size > 0 && !min_desc.empty?
    description += min_desc
    description += ', ' if description.size > 0 && !hour_desc.empty?
    description += hour_desc
  end
  description
end

#to_hashObject



29
30
31
# File 'lib/cronex/exp_descriptor.rb', line 29

def to_hash
  Hash[SEGMENTS.take(7).zip(expression_parts)]
end

#transform_case(desc, case_type = :lower) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/cronex/exp_descriptor.rb', line 171

def transform_case(desc, case_type = :lower)
  case case_type
  when :sentence
    desc.sub(/\b[[:word:]]/u) { |s| Unicode.upcase(s) }
  when :title
    desc.gsub(/\b[[:word:]]/u) { |s| Unicode.upcase(s) }
  else
    Unicode.downcase(desc)
  end
end

#transform_verbosity(desc, verbose = false) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/cronex/exp_descriptor.rb', line 182

def transform_verbosity(desc, verbose = false)
  return desc if verbose
  parts = %w(every_minute every_hour every_day)
  parts.each { |part| desc.gsub!(resources.get(part.gsub('_', '_1_')), resources.get(part)) }
  parts.each { |part| desc.gsub!(', ' + resources.get(part), '') }
  desc
end

#year_description(expression_parts) ⇒ Object



92
93
94
95
# File 'lib/cronex/exp_descriptor.rb', line 92

def year_description(expression_parts)
  desc = YearDescription.new(resources, options)
  desc.segment_description(expression_parts[6], ', ' + resources.get('every_year'))
end