Class: RRule::Frequency

Inherits:
Object
  • Object
show all
Defined in:
lib/rrule/frequencies/frequency.rb

Direct Known Subclasses

Daily, Monthly, SimpleWeekly, Weekly, Yearly

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, filters, generator, timeset, start_date: nil) ⇒ Frequency

Returns a new instance of Frequency.



7
8
9
10
11
12
13
# File 'lib/rrule/frequencies/frequency.rb', line 7

def initialize(context, filters, generator, timeset, start_date: nil)
  @context = context
  @current_date = start_date.presence || context.dtstart
  @filters = filters
  @generator = generator
  @timeset = timeset
end

Instance Attribute Details

#current_dateObject (readonly)

Returns the value of attribute current_date.



5
6
7
# File 'lib/rrule/frequencies/frequency.rb', line 5

def current_date
  @current_date
end

#filtersObject (readonly)

Returns the value of attribute filters.



5
6
7
# File 'lib/rrule/frequencies/frequency.rb', line 5

def filters
  @filters
end

#generatorObject (readonly)

Returns the value of attribute generator.



5
6
7
# File 'lib/rrule/frequencies/frequency.rb', line 5

def generator
  @generator
end

#timesetObject (readonly)

Returns the value of attribute timeset.



5
6
7
# File 'lib/rrule/frequencies/frequency.rb', line 5

def timeset
  @timeset
end

Class Method Details

.for_options(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rrule/frequencies/frequency.rb', line 35

def self.for_options(options)
  case options[:freq]
  when 'DAILY'
    Daily
  when 'WEEKLY'
    if options[:simple_weekly] && !options[:bymonth]
      SimpleWeekly # simplified and faster version if we don't need to deal with filtering
    else
      Weekly
    end
  when 'MONTHLY'
    Monthly
  when 'YEARLY'
    Yearly
  else
    raise InvalidRRule, 'Valid FREQ value is required'
  end
end

Instance Method Details

#advanceObject



15
16
17
18
19
# File 'lib/rrule/frequencies/frequency.rb', line 15

def advance
  @current_date = current_date.advance(advance_by).tap do |new_date|
    context.rebuild(new_date.year, new_date.month) unless same_month(current_date, new_date)
  end
end

#next_occurrencesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rrule/frequencies/frequency.rb', line 21

def next_occurrences
  possible_days_of_year = possible_days

  if filters.present?
    possible_days_of_year.each_with_index do |day_index, i|
      possible_days_of_year[i] = nil if filters.any? { |filter| filter.reject?(day_index) }
    end
  end

  generator.combine_dates_and_times(possible_days_of_year, timeset).tap do
    advance
  end
end