Class: IceCube::RuleOccurrence

Inherits:
Object
  • Object
show all
Defined in:
lib/ice_cube/rule_occurrence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ruleObject (readonly)

Returns the value of attribute rule.



74
75
76
# File 'lib/ice_cube/rule_occurrence.rb', line 74

def rule
  @rule
end

Instance Method Details

#all_occurrencesObject

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/ice_cube/rule_occurrence.rb', line 15

def all_occurrences
  raise ArgumentError.new("Rule must specify either an until date or a count to use 'all_occurrences'") unless @rule.occurrence_count || @rule.until_date || @end_time
  find_occurrences { |roc| false }
end

#between(begin_time, end_time) ⇒ Object



20
21
22
# File 'lib/ice_cube/rule_occurrence.rb', line 20

def between(begin_time, end_time)
  find_occurrences { |roc| roc > end_time }.select { |d| d >= begin_time }
end

#first(n) ⇒ Object



49
50
51
52
# File 'lib/ice_cube/rule_occurrence.rb', line 49

def first(n)
  count = 0
  find_occurrences { |roc| count += 1; count > n }
end

#next_occurrence(from) ⇒ Object

Break after the first occurrence after now



29
30
31
# File 'lib/ice_cube/rule_occurrence.rb', line 29

def next_occurrence(from)
  next_occurrences(1, from).first
end

#next_occurrences(n, from) ⇒ Object

Break after the first n occurrences after now



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ice_cube/rule_occurrence.rb', line 34

def next_occurrences(n, from)
  found_all = false
  num_found = 0
  nexts = find_occurrences do |roc|
    find = roc > from
    num_found += 1 if find
    success = found_all
    found_all = num_found == n
    success
  end
  #Since the above returns all up to and including the next N that were requested, we need
  #to grab the last n, making sure to prune out ones that were actually before the from time
  nexts.last(n).select{|occurrence| occurrence > from}
end

#succObject

get the next occurrence of this rule



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ice_cube/rule_occurrence.rb', line 55

def succ
  return nil if @rule.occurrence_count && @index >= @rule.occurrence_count # count check
  # get the next date to walk to
  if @date.nil?
    date = @start_date if @rule.validate_single_date(@start_date)
    date = @rule.next_suggestion(@start_date) unless date
  else
    date = @rule.next_suggestion(@date)
  end
  # walk through all of the successive dates, looking for the next occurrence (interval-valid), then return it.
  begin
    return nil if @end_time && date > @end_time
    return nil if @rule.until_date && date > @rule.until_date # until check
    next unless @rule.in_interval?(date, @start_date)
    return nil if yield(date)
    return RuleOccurrence.new(@rule, @start_date, @end_time, date, @index + 1)
  end while date = @rule.next_suggestion(date)
end

#to_timeObject

def <=>(other)

  to_time <=> other
end


11
12
13
# File 'lib/ice_cube/rule_occurrence.rb', line 11

def to_time
  @date
end

#upto(end_date) ⇒ Object



24
25
26
# File 'lib/ice_cube/rule_occurrence.rb', line 24

def upto(end_date)
  find_occurrences { |roc| roc > end_date }
end