Class: Cronline::CronDaysOfWeek

Inherits:
CronField show all
Defined in:
lib/cronline/cron_days_of_week.rb

Instance Method Summary collapse

Methods inherited from CronField

#time_field, wildcard

Constructor Details

#initialize(cron_expression) ⇒ CronDaysOfWeek

Returns a new instance of CronDaysOfWeek.



6
7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/cronline/cron_days_of_week.rb', line 20

def active?
  @field_expression != '?'
end

#last_weekday_day_of_month(weekday_number, time) ⇒ Object

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



41
42
43
44
45
46
# 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) ⇒ Object



24
25
26
27
28
29
30
# 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) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


49
50
51
52
53
54
# 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