Class: SayWhen::MonthsCronValue

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

Constant Summary collapse

MONTHS =
Date::ABBR_MONTHNAMES[1..-1].collect{|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.



406
407
408
# File 'lib/say_when/cron_expression.rb', line 406

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

Instance Method Details

#last(date) ⇒ Object



419
420
421
422
423
424
425
426
427
# File 'lib/say_when/cron_expression.rb', line 419

def last(date)
  last_month = self.values.reverse.detect{|v| v < date.month} 
  result = if last_month.nil?
    date.change(:year=>date.year - 1, :month=>self.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



429
430
431
432
433
434
435
436
437
# File 'lib/say_when/cron_expression.rb', line 429

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

#parse(exp) ⇒ Object



410
411
412
413
414
415
416
417
# File 'lib/say_when/cron_expression.rb', line 410

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