Class: SayWhen::MonthsCronValue

Inherits:
CronValue show all
Defined in:
lib/say_when/cron_expression.rb

Constant Summary collapse

MONTHS =
Date::ABBR_MONTHNAMES[1..-1].map { |a| a.upcase }

Instance Attribute Summary

Attributes inherited from CronValue

#expression, #max, #min, #part, #values

Instance Method Summary collapse

Methods inherited from CronValue

#include?, parse_number, #to_s

Constructor Details

#initialize(exp) ⇒ MonthsCronValue

Returns a new instance of MonthsCronValue.



369
370
371
# File 'lib/say_when/cron_expression.rb', line 369

def initialize(exp)
  super(:month, 1, 12, exp)
end

Instance Method Details

#last(date) ⇒ Object



380
381
382
383
384
385
386
387
388
# File 'lib/say_when/cron_expression.rb', line 380

def last(date)
  last_month = values.reverse.detect { |v| v < date.month }
  result = if last_month.nil?
    date.change(year: date.year - 1, month: values.last)
  else
    date.change(month: last_month)
  end
  result.change(day: result.end_of_month, hour: 23, min: 59, sec: 59)
end

#next(date) ⇒ Object



390
391
392
393
394
395
396
397
# File 'lib/say_when/cron_expression.rb', line 390

def next(date)
  next_month = values.detect { |v| v > date.month }
  if next_month.nil?
    date.change(year: date.year + 1, month: values.first, day: 1, hour: 0)
  else
    date.change(month: next_month, day: 1, hour: 0)
  end
end

#parse(exp) ⇒ Object



373
374
375
376
377
378
# File 'lib/say_when/cron_expression.rb', line 373

def parse(exp)
  if exp =~ /[A-Z]+/
    MONTHS.each_with_index { |mon, index| exp = exp.gsub(mon, (index + 1).to_s) }
  end
  super(exp)
end