Class: IceCube::RuleOccurrence

Inherits:
Object
  • Object
show all
Includes:
Comparable
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.



53
54
55
# File 'lib/ice_cube/rule_occurrence.rb', line 53

def rule
  @rule
end

Instance Method Details

#<=>(other) ⇒ Object

allow to be compared to dates



8
9
10
# File 'lib/ice_cube/rule_occurrence.rb', line 8

def <=>(other)
  to_time <=> other
end

#all_occurrencesObject

Raises:

  • (ArgumentError)


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

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



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

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

#first(n) ⇒ Object



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

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

#succObject

get the next occurrence of this rule



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

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 yield(date)
    return nil if @end_time && date > @end_time
    return nil if @rule.until_date && date > @rule.until_date # until check
    return RuleOccurrence.new(@rule, @start_date, @end_time, date, @index + 1) if @rule.in_interval?(date, @start_date)
  end while date = @rule.next_suggestion(date)
end

#to_timeObject



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

def to_time
  @date
end

#upto(end_date) ⇒ Object



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

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