Class: IceCube::Schedule

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date) ⇒ Schedule

Returns a new instance of Schedule.



7
8
9
10
11
12
13
# File 'lib/ice_cube/schedule.rb', line 7

def initialize(start_date)
  @rrule_occurrence_heads = []
  @exrule_occurrence_heads = []
  @rdates = []
  @exdates = []
  @start_date = start_date
end

Instance Attribute Details

#exdatesObject (readonly)

Returns the value of attribute exdates.



5
6
7
# File 'lib/ice_cube/schedule.rb', line 5

def exdates
  @exdates
end

#rdatesObject (readonly)

Returns the value of attribute rdates.



5
6
7
# File 'lib/ice_cube/schedule.rb', line 5

def rdates
  @rdates
end

Class Method Details

.from_hash(hash) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ice_cube/schedule.rb', line 29

def self.from_hash(hash)
  schedule = Schedule.new(hash[:start_date])
  hash[:rrules].each { |rr| schedule.add_recurrence_rule Rule.from_hash(rr) }
  hash[:exrules].each { |ex| schedule.add_exception_rule Rule.from_hash(ex) }
  hash[:rdates].each { |rd| schedule.add_recurrence_date rd }
  hash[:exdates].each { |ed| schedule.add_exception_date ed }
  schedule
end

.from_yaml(str) ⇒ Object



38
39
40
# File 'lib/ice_cube/schedule.rb', line 38

def self.from_yaml(str)
  from_hash(YAML::load(str))
end

Instance Method Details

#add_exception_date(date) ⇒ Object

Add an individual date exception to this schedule



98
99
100
# File 'lib/ice_cube/schedule.rb', line 98

def add_exception_date(date)
  @exdates << date
end

#add_exception_rule(rule) ⇒ Object

Add a rule of any type as an exception to this schedule

Raises:

  • (ArgumentError)


83
84
85
86
# File 'lib/ice_cube/schedule.rb', line 83

def add_exception_rule(rule)
  raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
  @exrule_occurrence_heads << RuleOccurrence.new(rule, @start_date)
end

#add_recurrence_date(date) ⇒ Object

Add an individual date to this schedule



93
94
95
# File 'lib/ice_cube/schedule.rb', line 93

def add_recurrence_date(date)
  @rdates << date
end

#add_recurrence_rule(rule) ⇒ Object

Add a rule of any type as an recurrence in this schedule

Raises:

  • (ArgumentError)


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

def add_recurrence_rule(rule)
  raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
  @rrule_occurrence_heads << RuleOccurrence.new(rule, @start_date)
end

#all_occurrencesObject

Return all possible occurrences In order to make this call, all rules in the schedule must have either an until date or an occurrence count



58
59
60
# File 'lib/ice_cube/schedule.rb', line 58

def all_occurrences
  find_occurrences { |head| head.all_occurrences }
end

#exrulesObject



88
89
90
# File 'lib/ice_cube/schedule.rb', line 88

def exrules 
  @exrule_occurrence_heads.map { |h| h.rule }
end

#first(n) ⇒ Object



67
68
69
70
# File 'lib/ice_cube/schedule.rb', line 67

def first(n)
  dates = find_occurrences { |head| head.first(n) }
  dates.slice(0, n)
end

#occurrences(end_date) ⇒ Object

Find all occurrences until a certain date



63
64
65
# File 'lib/ice_cube/schedule.rb', line 63

def occurrences(end_date)
  find_occurrences { |head| head.upto(end_date) }
end

#occurrences_between(begin_time, end_time) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ice_cube/schedule.rb', line 104

def occurrences_between(begin_time, end_time)
  exclude_dates, include_dates = Set.new(@exdates), SortedSet.new(@rdates)
  @rrule_occurrence_heads.each do |rrule_occurrence_head|
    include_dates.merge(rrule_occurrence_head.between(begin_time, end_time))
  end
  @exrule_occurrence_heads.each do |exrule_occurrence_head|
    exclude_dates.merge(exrule_occurrence_head.between(begin_time, end_time))
  end
  # reject all of the ones outside of the range
  include_dates.reject! { |date| exclude_dates.include?(date) || date < begin_time || date > end_time }
  include_dates.to_a
end

#occurs_at?(date) ⇒ Boolean

Determine whether a given time adheres to the ruleset of this schedule.

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/ice_cube/schedule.rb', line 43

def occurs_at?(date)
  dates = occurrences(date)
  dates.last == date
end

#occurs_on?(date) ⇒ Boolean

Determine whether a given date appears in the times returned by the schedule Required activeSupport

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/ice_cube/schedule.rb', line 50

def occurs_on?(date)
  time = date.to_time
  occurrences_between(time.beginning_of_day, time.end_of_day).any?
end

#rrulesObject



78
79
80
# File 'lib/ice_cube/schedule.rb', line 78

def rrules
  @rrule_occurrence_heads.map { |h| h.rule }
end

#to_hashObject



15
16
17
18
19
20
21
22
23
# File 'lib/ice_cube/schedule.rb', line 15

def to_hash
  hash = Hash.new
  hash[:start_date] = @start_date
  hash[:rrules] = @rrule_occurrence_heads.map { |rr| rr.rule.to_hash }
  hash[:exrules] = @exrule_occurrence_heads.map { |ex| ex.rule.to_hash }
  hash[:rdates] = @rdates
  hash[:exdates] = @exdates
  hash
end

#to_yamlObject



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

def to_yaml
  to_hash.to_yaml
end