Class: IceCube::Rule

Constant Summary collapse

SuggestionTypes =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HourOfDayValidation

#closest_hour_of_day, #hour_of_day, included, #validate_hour_of_day

Methods included from MinuteOfHourValidation

#closest_minute_of_hour, included, #minute_of_hour, #validate_minute_of_hour

Methods included from SecondOfMinuteValidation

#closest_second_of_minute, included, #second_of_minute, #validate_second_of_minute

Methods included from MonthOfYearValidation

#closest_month_of_year, included, #month_of_year, #validate_month_of_year

Methods included from DayOfYearValidation

#closest_day_of_year, #day_of_year, included, #validate_day_of_year

Methods included from DayOfMonthValidation

#closest_day_of_month, #day_of_month, included, #validate_day_of_month

Methods included from DayOfWeekValidation

#closest_day_of_week, #day_of_week, included, #validate_day_of_week

Methods included from DayValidation

#closest_day, #day, included, #validate_day

Instance Attribute Details

#occurrence_countObject (readonly)

Returns the value of attribute occurrence_count.



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

def occurrence_count
  @occurrence_count
end

#until_dateObject (readonly)

Returns the value of attribute until_date.



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

def until_date
  @until_date
end

#validationsObject

Returns the value of attribute validations.



119
120
121
# File 'lib/ice_cube/rule.rb', line 119

def validations
  @validations
end

Class Method Details

.daily(interval = 1) ⇒ Object

create a new daily rule



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

def self.daily(interval = 1)
  DailyRule.new(interval)
end

.from_hash(hash) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ice_cube/rule.rb', line 21

def self.from_hash(hash)
  rule = hash[:rule_type].split('::').inject(Object) { |namespace, const_name| namespace.const_get(const_name) }.new(hash[:interval])
  rule.count(hash[:count]) if hash[:count]
  rule.until(hash[:until]) if hash[:until]
  rule.validations = hash[:validations]
  rule
end

.hourly(interval = 1) ⇒ Object

create a new hourly rule



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

def self.hourly(interval = 1)
  HourlyRule.new(interval)
end

.minutely(interval = 1) ⇒ Object

create a new minutely rule



55
56
57
# File 'lib/ice_cube/rule.rb', line 55

def self.minutely(interval = 1)
  MinutelyRule.new(interval)
end

.monthly(interval = 1) ⇒ Object

create a new monthly rule



40
41
42
# File 'lib/ice_cube/rule.rb', line 40

def self.monthly(interval = 1)
  MonthlyRule.new(interval)
end

.secondly(interval = 1) ⇒ Object

create a new secondly rule



60
61
62
# File 'lib/ice_cube/rule.rb', line 60

def self.secondly(interval = 1)
  SecondlyRule.new(interval)
end

.weekly(interval = 1) ⇒ Object

create a new weekly rule



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

def self.weekly(interval = 1)
  WeeklyRule.new(interval)
end

.yearly(interval = 1) ⇒ Object

create a new yearly rule



45
46
47
# File 'lib/ice_cube/rule.rb', line 45

def self.yearly(interval = 1)
  YearlyRule.new(interval)
end

Instance Method Details

#count(count) ⇒ Object

set the number of occurrences after which this rule is no longer effective

Raises:

  • (ArgumentError)


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

def count(count)
  raise ArgumentError.new('Argument must be a positive integer') unless Integer(count) && count >= 0
  @occurrence_count = count
  self
end

#next_suggestion(date) ⇒ Object

The key - extremely educated guesses This spidering behavior will go through look for the next suggestion by constantly moving the farthest back value forward



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ice_cube/rule.rb', line 89

def next_suggestion(date)
  # get the next date recommendation set
  suggestions = SuggestionTypes.map { |r| send("closest_#{r}", date) }
  compact_suggestions = suggestions.compact
  # find the next date to go to
  if compact_suggestions.empty?
    next_date = date
    loop do
      # keep going through rule suggestions
      next_date = self.default_jump(next_date)
      return next_date if validate_single_date(next_date)
    end
  else  
    loop do
      compact_suggestions = suggestions.compact
      min_suggestion = compact_suggestions.min
      # validate all against the minimum
      return min_suggestion if validate_single_date(min_suggestion)
      # move anything that is the minimum to its next closest
      SuggestionTypes.each_with_index do |r, index|
        suggestions[index] = send("closest_#{r}", min_suggestion) if min_suggestion == suggestions[index]
      end
    end
  end
end

#to_hashObject



11
12
13
14
15
16
17
18
19
# File 'lib/ice_cube/rule.rb', line 11

def to_hash
  hash = Hash.new
  hash[:rule_type] = self.class.name
  hash[:interval] = @interval
  hash[:until] = @until_date
  hash[:count] = @occurrence_count
  hash[:validations] = @validations
  hash
end

#to_sObject



115
116
117
# File 'lib/ice_cube/rule.rb', line 115

def to_s
  to_ical
end

#until(until_date) ⇒ Object

Set the time when this rule will no longer be effective

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/ice_cube/rule.rb', line 65

def until(until_date)
  raise ArgumentError.new('Cannot specify until and count on the same rule') if @count #as per rfc
  raise ArgumentError.new('Argument must be a valid Time') unless until_date.class == Time
  @until_date = until_date
  self
end

#validate_single_date(date) ⇒ Object



79
80
81
82
83
84
# File 'lib/ice_cube/rule.rb', line 79

def validate_single_date(date)
  SuggestionTypes.all? do |s|
    response = send("validate_#{s}", date)
    response.nil? || response
  end
end