Class: RRule::Rule

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rrule/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rrule, dtstart: Time.now, tzid: 'UTC', exdate: [], max_year: nil) ⇒ Rule

Returns a new instance of Rule.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rrule/rule.rb', line 9

def initialize(rrule, dtstart: Time.now, tzid: 'UTC', exdate: [], max_year: nil)
  @tz = tzid
  @rrule = rrule
  @dtstart = dtstart.is_a?(Date) ? dtstart : floor_to_seconds_in_timezone(dtstart)
  @exdate = exdate
  @options = parse_options(rrule)
  @frequency_type = Frequency.for_options(options)
  @max_year = max_year || 9999
  @max_date = DateTime.new(@max_year)
end

Instance Attribute Details

#dtstartObject (readonly)

Returns the value of attribute dtstart.



7
8
9
# File 'lib/rrule/rule.rb', line 7

def dtstart
  @dtstart
end

#exdateObject (readonly)

Returns the value of attribute exdate.



7
8
9
# File 'lib/rrule/rule.rb', line 7

def exdate
  @exdate
end

#tzObject (readonly)

Returns the value of attribute tz.



7
8
9
# File 'lib/rrule/rule.rb', line 7

def tz
  @tz
end

Instance Method Details

#all(limit: nil) ⇒ Object



24
25
26
# File 'lib/rrule/rule.rb', line 24

def all(limit: nil)
  all_until(limit: limit)
end

#between(start_date, end_date, limit: nil) ⇒ Object



28
29
30
31
32
# File 'lib/rrule/rule.rb', line 28

def between(start_date, end_date, limit: nil)
  floored_start_date = floor_to_seconds_in_timezone(start_date)
  floored_end_date = floor_to_seconds_in_timezone(end_date)
  all_until(start_date: floored_start_date, end_date: floored_end_date, limit: limit).reject { |instance| instance < floored_start_date }
end

#each(floor_date: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rrule/rule.rb', line 39

def each(floor_date: nil)
  # If we have a COUNT or INTERVAL option, we have to start at dtstart, because those are relative to dtstart
  floor_date = dtstart if count_or_interval_present? || floor_date.nil? || dtstart > floor_date

  return enum_for(:each, floor_date: floor_date) unless block_given?
  context = Context.new(options, dtstart, tz)
  context.rebuild(floor_date.year, floor_date.month)

  timeset = options[:timeset]
  count = options[:count]

  filters = []
  filters.push(ByMonth.new(options[:bymonth], context)) if options[:bymonth]

  filters.push(ByWeekNumber.new(options[:byweekno], context)) if options[:byweekno]

  filters.push(ByWeekDay.new(options[:byweekday], context)) if options[:byweekday]

  filters.push(ByYearDay.new(options[:byyearday], context)) if options[:byyearday]

  filters.push(ByMonthDay.new(options[:bymonthday], context)) if options[:bymonthday]

  generator = if options[:bysetpos]
    BySetPosition.new(options[:bysetpos], context)
  else
    AllOccurrences.new(context)
  end

  frequency = Frequency.for_options(options).new(context, filters, generator, timeset, start_date: floor_date)

  loop do
    return if frequency.current_date.year > max_year

    frequency.next_occurrences.each do |this_result|
      next if this_result < dtstart
      next if floor_date.present? && this_result < floor_date
      return if options[:until] && this_result > options[:until]
      return if count && (count -= 1) < 0
      yield this_result unless exdate.include?(this_result)
    end
  end
end

#from(start_date, limit:) ⇒ Object



34
35
36
37
# File 'lib/rrule/rule.rb', line 34

def from(start_date, limit:)
  floored_start_date = floor_to_seconds_in_timezone(start_date)
  all_until(start_date: floored_start_date, limit: limit).reject { |instance| instance < floored_start_date }
end

#humanizeObject



86
87
88
# File 'lib/rrule/rule.rb', line 86

def humanize
  Humanizer.new(self, options).to_s
end

#is_finite?Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/rrule/rule.rb', line 90

def is_finite?
  keys = %i[count until] & options.keys
  !keys.empty?
end

#nextObject



82
83
84
# File 'lib/rrule/rule.rb', line 82

def next
  enumerator.next
end

#to_sObject



20
21
22
# File 'lib/rrule/rule.rb', line 20

def to_s
  @rrule
end