class Cronline::CronDaysOfWeek

Public Class Methods

new(cron_expression) click to toggle source
Calls superclass method
# File lib/cronline/cron_days_of_week.rb, line 6
def initialize(cron_expression)
  @field_expression = cron_expression.split(' ')[5]
  Date::ABBR_DAYNAMES.each do |abbreviation|
    unless abbreviation.nil?
      @field_expression.gsub!(/(#{abbreviation})/i, Date::ABBR_DAYNAMES.index(abbreviation).to_s)
    end
  end
  if @field_expression[-1,1] == 'L'
    @last_weekday_of_month = true
    @field_expression.sub!('L', '')
  end
  super(@field_expression, 1, 7)
end

Public Instance Methods

active?() click to toggle source
# File lib/cronline/cron_days_of_week.rb, line 20
def active?
  @field_expression != '?'
end
last_weekday_day_of_month(weekday_number, time) click to toggle source

Thank you Mohit Sindhwani www.ruby-forum.com/topic/125970

# File lib/cronline/cron_days_of_week.rb, line 41
def last_weekday_day_of_month(weekday_number, time)
  last_day_of_month = Date.new(time.year, time.month, -1)
  last_day_wday = ((last_day_of_month.wday + 1) - weekday_number).abs
  last_day = Date.new(time.year, time.month, (last_day_of_month.day - last_day_wday))
  last_day.day
end
range(time) click to toggle source
Calls superclass method
# File lib/cronline/cron_days_of_week.rb, line 24
def range(time)
  if @last_weekday_of_month
    [last_weekday_day_of_month(super(time)[0], time)]
  else
    super(time)
  end
end
test?(time) click to toggle source
# File lib/cronline/cron_days_of_week.rb, line 32
def test?(time)
  if @last_weekday_of_month
    test_last_weekday?(@range[0], time)
  else
    @range.include?(time.wday)
  end
end
test_last_weekday?(weekday_number, time) click to toggle source

Thank you Mohit Sindhwani www.ruby-forum.com/topic/125970

# File lib/cronline/cron_days_of_week.rb, line 49
def test_last_weekday?(weekday_number, time)
  last_day_of_month = Date.new(time.year, time.month, -1)
  last_day_wday = ((last_day_of_month.wday + 1) - weekday_number).abs
  last_day = Date.new(time.year, time.month, (last_day_of_month.day - last_day_wday))
  last_day.year == time.year && last_day.month == time.month && last_day.day == time.day
end